FileUpload1.HasFile is always returning false - c#

I am uploading file using ASP.net File upload control.
My FileUpload1.HasFile is always returning false.
if (FileUpload1.HasFile)
{
DBOperations db = new DBOperations();
try
{
FileUpload1.SaveAs(Server.MapPath("~/uploadedImages/" + db.uploadImage(System.IO.Path.GetExtension(FileUpload1.FileName)) + System.IO.Path.GetExtension(FileUpload1.FileName)));
}
catch (Exception Ex)
{
String he = Ex.Message;
}
}
I am using following ASP.net Code
<asp:UpdatePanel ID="fileUpload" runat="server">
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUploadFile" EventName="Click" />
</Triggers>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUploadFile" Text="Upload File" runat="server"
onclick="btnUploadFile_Click" />
<br />
<asp:RegularExpressionValidator ID="revImage" ControlToValidate="FileUpload1" ValidationExpression="^.*\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$" Text="Invalid image type" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
I have tried even by removing AsyncPostBackTrigger and even by removing Whole asp:updatePanel then also my FileUpload1.HasFile always returns false.

ASP.NET's "AJAX" thing doesn't support file uploads in UpdatePanels out of the box. Change that trigger into a PostBackTrigger (which causes a full page load) or use something else to upload the file.

Add a trigger for your UpdatePanel
<Triggers>
<asp:PostBackTrigger ControlID="btnUploadFile" />
</Triggers>
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");

I know this post if old, but I found that if the file is empty [ 0 KB ] then it will return false as well. There has to be something in the file in order for .HasFile to acknowledge it.

Related

update panel not working in where upload excel sheet

If i am using update panel then PostedFile not giving file path some error are coming and if update panel are removed the page then excel file successfully upload how to solve
your code should be as shown below
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload runat="server" ID="flFile" />
<asp:Button runat="server" ID="btnSubmit" Text="Upload" OnClick="btnSubmit_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
You need to put postback trigger if you are uploading any files.
A FileUpload inside UpdatePanel won't work. You have to post the whole page. You do this by adding a PostBackTrigger to the button you use for uploading files.
Something like this (see Triggers):
<asp:UpdatePanel ID="upnlMain" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="fileUpload" runat="server" Width="400px" />
<asp:Button ID="btnUploadFiles" runat="server" Text="Upload files" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnUploadFiles" />
</Triggers>
</asp:UpdatePanel>
If you have problems with not posting the first time a button is clicked then add this to Page Load event:
Page.Form.Attributes.Add("enctype", "multipart/form-data");

Unable to upload photo asynchronously with FileUpload control using UpdatePanel

In my ASP.NET Web forms application, I am adding a feature to upload files but I don't want to do a full postback. So to make it asynchronous I thought of using UpdatePanel.
This UpdatePanel contains a FileUpload control and a button to upload the selected photo. When I was debuggin my code to detect if file is actually selected or not, I found FileUpload's HasFile property to be false.
It works when I remove UpdatePanel but I don't understand why.
To help you understand, here is the code:
ASPX markups:
<asp:UpdatePanel ID="UPProf" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUDP" runat="server" />
<asp:Button ID="BUDP" runat="server" OnClick="BUDP_Click" Text="Upload your photo" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BUDP" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Here's its CS code:
protected void BUDP_Click(object sender, EventArgs e)
{
try
{
if (FUDP.HasFile) // code doesn't pass this if condition
{
FUDP.SaveAs("D:/Pictures/" + count + ".jpeg"); //it doesn't work since there is no file here
}
else
{
//Set some message for user
}
}
catch (Exception ex)
{
//log the error
}
}
Upload requires full page request. This is a limitation of the XmlHttpRequest component used in all AJAX frameworks for asynchronous calls to the application.
Remove the UpdatePanel or make BUDP button to perform postbacks.
<asp:UpdatePanel ID="UPProf" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FUDP" runat="server" />
<asp:Button ID="BUDP" runat="server" OnClick="BUDP_Click"
Text="Upload your photo" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="BUDP" />
</Triggers>
</asp:UpdatePanel>

asp.net FileUpload doesn't work properly

I have asp:FileUpload , two asp:Button , and GridView in UpldatePanel.
<asp:FileUpload runat="server" ID="fileExcelUpload" />
<asp:Button ID="btnShow" runat="server" Width="80px" OnClick="btnShow_Clicked"
AutoPostBack="true" Text="Save"/>
<asp:Button ID="btnImport" runat="server" Width="80px" OnClick="btnImport_Clicked"
AutoPostBack="true" Text="Save"/>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
.........GridView...........
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnShow" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnImport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Firstly , I retrieve some data from database and bind to gridView using btnShow.It works perfectly.
But when I upload Excel file with asp:FileUpload and click btnImport ,
protected void btnImport_Clicked(object sender, EventArgs e)
{
if (fileUExcelUpload.HasFile)
{
.....
}
}
the boolean value fileUExcelUpload.HasFile return False .
But when I remove <asp:AsyncPostBackTrigger ControlID="btnImport" EventName="Click" /> from UpldatePanel's triggers , it return True .
(The reason why I add btnImport's Click event to UpdatePanel's Triggers is , I want to persist Uploaded File of asp:FileUpload after postback . )
Is there any right way to do it ? Kindly help me Please :) Thanks !
Add this line in the
<Triggers> </Triggers>
<asp:PostBackTrigger ControlID="btnUpload" />
here btnUpload is the id of the fileupload control

How to upload an image when fileupload control is under updatepanel?

How to upload an image when fileupload is under updatepanel?
I have a button say "upload" inside that update panel.
When I click that button inside the button click event I got the fileupload hasfile=false.
Please suggest if there is any way to upload image file when fileupload control is inside update panel and the button is making asyncpostback.
Thanks in advance.
you can use AJAX AsyncFileUpload control
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx
Also check this thread.
File uploading in AJAX updatepanel without full postback
It is not possible. For security reasons, the browser does not allow javascript to upload files.
There are two normal workarounds for this problem:
Perform the upload in an iFrame
Use a flash plugin for uploading
I recently applied this tool to upload files asynchronously in my web page, and it works beatifully: http://valums.com/ajax-upload/
It creates an iFrame for you automatically, and posts the frame and and sends the resulting html (or json object) to an event handler. My page that receives the uploaded file, returns a json object describing the file, e.g. filename and a unique id, so that I can link the data that is posted on the main page to the uploaded file.
For security, I store the credentials of the user uploading the file, so when the form is posted I can validate that the user that posts the form is indeed the user that uploaded the file.
I had tried as below. It is working.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Upload" runat="server" Text="Upload" OnClick="Upload_Click" /><br />
<asp:Image ID="NormalImage" runat="server" /></ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Upload" />
</Triggers>
</asp:UpdatePanel>
Reference to
http://www.c-sharpcorner.com/uploadfile/prathore/fileupload-control-in-update-panel-using-Asp-Net-ajax/
You can try this code. I tried it, it's working on my side.
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /> <br/>
<asp:Button ID="btn_Upload" runat="server" Text="Save" OnClick="btn_Upload_Click" /><br />
</div>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_Upload" />
</Triggers>
</asp:UpdatePanel>

FileUpload Doesn't Work When Nested In UpdatePanel? C#

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload onchange="clickTheButton();" ID="FileUpload1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
Button 1 is outside the update panel and the javascript that gets run when a user adds a file in the upload box is this:
function clickTheButton() {
document.getElementById('<%= Button1.ClientID %>').click();
}
The problem is simple. FileUpload1.HasFile == false. I don't know why this is, but when I put it in the update panel it ceases to work.
I have seen some other threads on this. But none of them answer why this is happening, they just point to things you can download.
EDIT: Really my main reason for wanting to do this is so that I can get a ..Uploading File.. Tag to pop up while the client is uploading to the server and once it has completed, display it in a datalist. I just cant get the UpdateProgress to work.
Basically you just need to make your button do a full postback to send the file. Also make sure that you have this.Form.Enctype = "multipart/form-data"; set in your code, or you can put in that page. AsyncPostbacks don't work with files for security reasons as mentioned, without hacks. (I've never been able to get it to work).
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload onchange="clickTheButton();" ID="FileUpload1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
For security purposes, browsers don't let you post files via javascript. Imagine if I could write a little bit a javascript to asynchronously submit the contents of your My Documents folder to my server.
So javascript-ish methods of posting the form, like the XMLHttpRequest used by the UpdatePanel, won't work.
This post describes a decent work around if you're on 3.5 SP1. http://geekswithblogs.net/ranganh/archive/2009/10/01/fileupload-in-updatepanel-asp.net-like-gmail.aspx
And this post describes a couple work arounds if you'd prefer not to use the AjaxControlToolkit. http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx
File Upload will not work with a partial post back.
So just add this line at your page load
ScriptManager.GetCurrent(this).RegisterPostBackControl(this.YourControlID);
Or use PostBackTrigger.
<Triggers>
<asp:PostBackTrigger ControlID="YourControlID" />
</Triggers>
Or You need special AsyncFileUpload control as defined in AjaxControl Toolkit.
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError"
OnClientUploadComplete="uploadComplete" runat="server"
ID="AsyncFileUpload1" Width="400px" UploaderStyle="Modern"
UploadingBackColor="#CCFFFF" ThrobberID="myThrobber" />
You can check here.

Categories