asp.net文件上传控件 [英] asp.net file upload control

查看:68
本文介绍了asp.net文件上传控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

i在我的webform中使用了一个fileupload控件但是当我在fileupload控件中选择任何文件时它的属性

Fileupload1.HasFile显示false是什么问题我的代码是



hello,
i am using a fileupload control in my webform but when i select any file in fileupload control then its property
Fileupload1.HasFile showing false what is the problem my code is

if(FileUpload1.HasFile)
        {
            string filename = FileUpload1.FileName; 
            string imagepath = "Uploads/" + filename;
            FileUpload1.SaveAs(Server.MapPath("Uploads/" + filename));
            values[4] = filename;
            values[5] = imagepath;
            obj_msg.fun_Insertmessagewithattachment(values);
        }





只是因为if(FileUpload1.HasFile)这个条件不正确我的代码没有运行

请帮助我....



谢谢&问候

Srishti Gupta

推荐答案

任何时候使用文件上传控件和最好的解决方案是使用更新面板来减少此类错误。我希望它能正常工作...... :-)



Any time it will be happen when working with file upload control and the best solution is to use update panel to reduce this kind of error. I hope its will be working properly...:-)

<table width="100%" align="center" cellpadding="0" cellspacing="2">
                                       <tr>
                                           <td class="style2">
                                               Enter Access Path :
                                           </td>
                                           <td>
                                               &nbsp; &nbsp;
                                               <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                               <ContentTemplate>
                                               <asp:FileUpload ID="FileUpload1" runat="server" />
                                               <asp:Button ID="cmd_getpath" runat="server" OnClick="cmd_getpath_Click" Text="Get Path" />
                                               <asp:TextBox ID="txtPath" runat="server"></asp:TextBox>
                                               </ContentTemplate>
                                                   <Triggers >
                                                   <asp:PostBackTrigger ControlID="cmd_getpath" />
                                                   </Triggers>
                                               </asp:UpdatePanel>
                                           </td>
                                       </tr>
</table>


问题



参考 - FileUpload类 [ ^ ]。

Problem

Refer - FileUpload Class[^].
Quote:

FileUpload控件旨在仅在回发场景中使用,而不是在部分页面呈现期间的异步回发场景中使用。在 UpdatePanel [ ^ ]控件,文件必须使用 PostBackTrigger [ ^ ]面板的对象。

The FileUpload control is designed to be used only in postback scenarios and not in asynchronous postback scenarios during partial-page rendering. When you use a FileUpload control inside an UpdatePanel[^] control, the file must be uploaded by using a control that is a PostBackTrigger[^] object for the panel.



解决方案





  • FileUpload1 添加 PostBackTrigger

    .
    Solution


    • Add PostBackTrigger for FileUpload1.

      <Triggers>
          <asp:Postbacktrigger controlid="FileUpload1" />
      </Triggers>

    • 使用 FileName 属性检查文件是否存在。

    • Use FileName property to check whether the File is present or not.

      if (FileUpload1.FileName != string.Empty)
      {
            //do something
      }




    • 这篇关于asp.net文件上传控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆