FileUpload.hasFile is always False

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

I have a FileUpload control (and it's not inside an UpdatePanel) and its hasFile property is always False.

   

Any thought?

回答1:

Add a trigger for your UpdatePanel

This will force a postback when the upload button is clicked.

Also add the line below to the Page_Load

Page.Form.Attributes.Add("enctype", "multipart/form-data"); 


回答2:

You cannot upload files using AJAX => you should not be placing a FileUpload control inside an UpdatePanel because this UpdatePanel sends an AJAX request to the server.



回答3:

I also uploaded a file using the FileUpload control, but the HasFile property returned false. Turn out that FileUpload.HasFile is also false if you upload an empty file. In this case adding some text to the file you want to upload will make the Hasfile property return true.



回答4:

To complement the example given by @dbFrameIT Support:

                                      

your code behind (c#)

    protected void UploadButton_Click(object sender, EventArgs e)     {         if (FileUpload1.HasFile == false)         {             UploadDetails.Text = "Please first select a file to upload...";         }         else         {             string FileName = FileUpload1.FileName;             UploadDetails.Text = string.Format(                     @"Uploaded file: {0}
File size (in bytes): {1:N0}
Content-type: {2}", FileName, FileUpload1.FileBytes.Length, FileUpload1.PostedFile.ContentType); // Save the file string filePath = Server.MapPath("~/Brochures/" + FileUpload1.FileName); FileUpload1.SaveAs(filePath); } }


回答5:

the whole time it was about the permissions i had(or didn't have to be more specific) over the file am trying to upload, i granted the user the sufficient permissions and it all went well.

thanks a lot for your help and posts.



回答6:

Sometimes with fileUpload has problems. Instead it you can use simple input:

In code save file to server:

HttpPostedFile myFile = filMyFile.PostedFile;            string fullPath=Server.MapPath("~/UploadDocuments/") + myFile.FileName; myFile.SaveAs(fullPath); 

And file will save at UploadDocuments folder in your ASP.NET application (server)



回答7:

You can try to take your button off from the UpdatePanel; As far as I get, UpdatePanels always update when something inside any other update panel updates, so if your button postback, your FileUpload control also postback and lose the file reference.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!