Reputation: 699
I have a file upload control and a button:
<asp:FileUpload ID="venfileupld" runat="server" />
<asp:Button ID="venupld1" runat="server" Text="Upload" OnClick="venupld1_Click" />
In the button click event I am doing this:
string name = venfileupld.PostedFile.FileName;
string filepath = Server.MapPath("upload_excel/") + name;
venfileupld.PostedFile.SaveAs(filepath);
writetoven();
But it's giving me an error.
My first question is why I am getting an error on the line:
string name = venfileupld.PostedFile.FileName;
It's giving null that the uploader doesn't have any file.
And the second question is how I get the file that I can pass on the function named:
writetoven();
Any help?
Upvotes: 1
Views: 828
Reputation: 460058
Are you using ASP.NET Ajx with an UpdatePanel? ... yes i am using ajax and updatepanel
FileUpload doesn't work inside of an UpdatePanel
. You must use an AsyncFileUpload from the ASPNET AJAX control Toolkit.
Look here for more details: http://knowledgebaseworld.blogspot.de/2009/02/file-upload-not-working-with-update.html
You should handle the UploadedComplete
event of the AsyncFileUpload
control:
private static List<string> allowedExtensions = new List<string>(new string[] {
".xls",
".xlsx"
});
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (e.state == AjaxControlToolkit.AsyncFileUploadState.Success) {
string fileExt = System.IO.Path.GetExtension(e.filename);
if (allowedExtensions.Contains(fileExt)) {
string fileName = System.IO.Path.GetFileName(e.filename);
string dir = Server.MapPath("upload_excel/");
string path = Path.Combine(dir, fileName);
AsyncFileUpload1.PostedFile.SaveAs(path);
AsyncFileUpload1.FileContent.Close();
} else {
// wrong extension
}
} else {
// log and show error
}
}
Upvotes: 0
Reputation: 3721
Try Following:
Code behind:
protected void venupld1_Click(object sender, EventArgs e)
{
string name = venfileupld.FileName;
string filepath = Server.MapPath("~/upload_excel/") + name;
venfileupld.PostedFile.SaveAs(filepath);
}
Code inline:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:FileUpload ID="venfileupld" runat="server" />
<asp:Button ID="venupld1" runat="server" Text="Upload" OnClick="venupld1_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="venupld1" />
</Triggers>
</asp:UpdatePanel>
Hope this helps.
Upvotes: 0
Reputation: 5806
First of all, make sure you are using enctype=multipart/form-data
in your form tag. That will allow file upload controls to be posted to asp.net server and you will start receiving data in venfileupld.PostedFile
object and other properties.
Use venfileupld.HasFile
and venfileupld.ContentLength
properties to make sure file is uploaded and is not 0kb.
-- update --
Based on the fact that you are using update panel, you can try adding a postback trigger like
<Triggers> <asp:PostBackTrigger ControlID="btnUploadControlId" /> </Triggers>
----- end of update ----
Add more details if this doesn't solve your problem.
Upvotes: 0
Reputation: 285
You have to check upload control has file by its property "HasFile" before assign a file name to variable "name" you can see best example from following link : http://asp-net-example.blogspot.in/2008/10/fileupload-control-example.html
Upvotes: 1