如何在上传之前显示上传的文件名? [英] how to display uploaded files name before uploading it?
本文介绍了如何在上传之前显示上传的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示文件名(逐个)..我已经给出了我的代码,但它会抛出错误
i used .net framework 4.0
I want to show the file names(one by one).. I have given my codes but it throws error
i used .net framework 4.0
protected void btnUpload_Click2(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
if (FileUpload1.HasFile)
{
foreach (var file in FileUpload1.PostedFile)
{
//upload logic
Response.Write(file.FileName + " - " + file.ContentLength + " Bytes. <br />");
string FileName = System.IO.Path.GetExtension(PostedFile.FileName);
PostedFile.SaveAs(Server.MapPath("~/files") + PostedFile.FileName);
}
}
}
}
}
推荐答案
您好,
您的代码应该修改如下:
在aspx文件中你放置此代码:
Hello,
Your code should be modified as bellow:
In aspx file you place this code:
<form id="form1" runat="server">
<div>
<input type="file" multiple="true" runat="server" id="FileUpload2" />
<asp:Button runat="server" ID="btnUpload" Text="Upload" OnClick="btnUpload_Click"/>
</div>
</form>
在代码后面放置此代码:
And in code behind place this code:
protected void btnUpload_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile postedFile = Request.Files[i];
if (postedFile.ContentLength > 0)
{
//upload logic
var fileName = System.IO.Path.GetFileName(postedFile.FileName);
Response.Write(fileName + " - " + postedFile.ContentLength + " Bytes. <br />");
var savefilePath = Server.MapPath("~/files/") + fileName;
postedFile.SaveAs(savefilePath);
}
}
}
快乐编码!。
Happy coding!.
这篇关于如何在上传之前显示上传的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文