ASP.NET FileUpload in UpdatePanel - still not working

后端 未结 4 2019
梦如初夏
梦如初夏 2020-11-30 12:39

Attempting to use a FileUpload or AsyncFileUpload control in an updatepanel on a NET 4.5/C# web application.

I\'ve tried using either standard Scriptmanager or ToolK

4条回答
  •  时光说笑
    2020-11-30 13:16

    FileUpload

    FileUpload requires full page request. This is a limitation of the XmlHttpRequest component used in all AJAX frameworks for asynchronous calls to the application.

    What really throws me is that I have this working in another project (scriptmanager in masterpage, Fileupload in updatepanel, SaveButton is PostbackTrigger).

    I think you are using Full PostBack, although FileUpload is inside **UpdatePanel.

    For example,

    
       
          
          
       
       
          
       
    
    

    AsyncFileUpload

    If you use AsyncFileUpload with UpdatePanel, AsyncFileUpload.HasFile should only checked inside UploadedComplete (you cannot check inside Button click event).

    The reason is AsyncFileUpload is uploaded the file via Async by itself.

    Note: make sure you use ToolkitScriptManager instead of ScriptManager

    
    
        
            
            
    private string FileName { get { return (string)(Session["FileName"] ?? ""); } set { Session["FileName"] = value; } } protected void SaveButton_Click(object sender, EventArgs e) { string fileName = FileName; string path = Server.MapPath("~/App_Data/"); var fileInfo = new FileInfo(path + FileName); } protected void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e) { if (AsyncFileUpload1.HasFile) { FileName = AsyncFileUpload1.FileName; string path = Server.MapPath("~/App_Data/"); AsyncFileUpload1.SaveAs(path + AsyncFileUpload1.FileName); } }

    Other Thoughts

    I personally do not like using AsyncFileUpload inside UpdatePanel. Instead, I'll rather use Full PostBack if I need to upload a file.

提交回复
热议问题