[英]Error while trying to upload a file
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? 有什么帮助吗?
您必须先通过属性“ HasFile”检查上传控件的文件,然后再将文件名分配给变量“ name”,然后才能通过以下链接查看最佳示例: http : //asp-net-example.blogspot.in/2008/10 /fileupload-control-example.html
First of all, make sure you are using enctype=multipart/form-data
in your form tag. 首先,请确保在表单标签中使用
enctype=multipart/form-data
。 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. 这样可以将文件上传控件发布到asp.net服务器,并且您将开始在
venfileupld.PostedFile
对象和其他属性中接收数据。
Use venfileupld.HasFile
and venfileupld.ContentLength
properties to make sure file is uploaded and is not 0kb. 使用
venfileupld.HasFile
和venfileupld.ContentLength
属性来确保文件已上传且不为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. 如果这不能解决您的问题,请添加更多详细信息。
Are you using ASP.NET Ajx with an UpdatePanel?
您是否将ASP.NET Ajx与UpdatePanel一起使用? ... yes i am using ajax and updatepanel
...是的,我正在使用ajax和updatepanel
FileUpload doesn't work inside of an UpdatePanel
. FileUpload在
UpdatePanel
内部不起作用。 You must use an AsyncFileUpload from the ASPNET AJAX control Toolkit . 您必须使用ASPNET AJAX控件工具包中的AsyncFileUpload 。
Look here for more details: http://knowledgebaseworld.blogspot.de/2009/02/file-upload-not-working-with-update.html 在此处查看更多详细信息: http : //knowledgebaseworld.blogspot.de/2009/02/file-upload-not-working-with-update.html
You should handle the UploadedComplete
event of the AsyncFileUpload
control: 您应该处理
AsyncFileUpload
控件的UploadedComplete
事件:
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
}
}
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. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.