C# 使用 "<input type="file" .... />" 而不是 asp:FileUpload
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16217705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using "<input type="file" .... />" instead of asp:FileUpload
提问by kmarks2
I'm modifying an existing ASP.NET project. The original author erroneously tried to create a styled asp:FileUpload by setting its visibility to hidden and just creating two custom styled browse and save buttons.
我正在修改现有的 ASP.NET 项目。原作者错误地尝试通过将其可见性设置为隐藏并仅创建两个自定义样式的浏览和保存按钮来创建样式化的 asp:FileUpload。
For security reason, IE does not permit this. My strategy is to instead try to use input tags with type="file", like this example. So if I set up the input like <input type="file" ID="inputFile" />how do I access/save the file in my code behind, inputFile.SaveAs("someFile.txt");? Also (in code behind) can I do something like inputFile.HasFileor is there some other analog of this?
出于安全原因,IE 不允许这样做。我的策略是尝试使用 type="file" 的输入标签,就像这个例子一样。因此,如果我设置输入,例如<input type="file" ID="inputFile" />如何在我的代码后面访问/保存文件,inputFile.SaveAs("someFile.txt");?另外(在后面的代码中)我可以做类似的事情inputFile.HasFile还是有其他类似的东西?
As per recommendations I'm trying something like the following:
根据建议,我正在尝试以下操作:
<td>
Enabled: <asp:CheckBox ID="CheckBox2" runat="server" />
<div id="testFileUploader">>
<input type="file" id="browserHidden" runat="server" />
<div id="browserVisible"><input type="text" id="fileField" /></div>
</div>
</td>
回答by melancia
Add a runat="server" to the object. This way it will work on the CodeBehid just like any asp:FileUpload control.
向对象添加 runat="server"。这样它就可以像任何 asp:FileUpload 控件一样在 CodeBehid 上工作。
回答by Abdullah SARGIN
if(fileUrunResim.HasFile)
fileUrunResim.SaveAs(MapPath("~/Images/" + fileUrunResim.FileName));
**if you unique filename,**
string extension = Path.GetExtension(fileUrunResim.FileName);
string fileName = Guid.NewGuid().ToString().Substring(0, 25) + extension ;
if(fileUrunResim.HasFile)
fileUrunResim.SaveAs(MapPath("~/Images/" + filename ));
回答by MikePR
As commented you can add the runat="server" to you input file tag.
正如所评论的,您可以将 runat="server" 添加到您的输入文件标签中。
By another hand, there is already a similar post about what you're asking for. Check this out:
另一方面,已经有一篇关于您要求的类似帖子。看一下这个:
Uploading Files in ASP.net without using the FileUpload server control
在 ASP.net 中上传文件而不使用 FileUpload 服务器控件
Hope this help
希望这有帮助
Cheers!
干杯!
回答by Secret
So, you can generate a random file name for the a future upload, based on the GUIDat the CodeBehindof ASPX page:
所以,你可以基于对未来的上传一个随机文件名,则GUID在CodeBehindASPX页面:
HttpPostedFile filePosted = Request.Files["uploadFieldNameFromHTML"];
if (filePosted != null && filePosted.ContentLength > 0)
{
string fileNameApplication = System.IO.Path.GetFileName(filePosted.FileName);
string fileExtensionApplication = System.IO.Path.GetExtension(fileNameApplication);
// generating a random guid for a new file at server for the uploaded file
string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
// getting a valid server path to save
string filePath = System.IO.Path.Combine(Server.MapPath("uploads"), newFile);
if (fileNameApplication != String.Empty)
{
filePosted.SaveAs(filePath);
}
}
For Request.Files["uploadFieldNameFromHTML"]set the ID in HTML code here:
对于Request.Files["uploadFieldNameFromHTML"]设置在这里的HTML代码中的ID:
<input type='file' id='...' />
Also, don't forget to define runat="server"at the main form in ASPX page, it's better to set it at the main form and don't forget about enctype="multipart/form-data"parameter of the <form>:
另外,不要忘记runat="server"在ASPX页面的主窗体中定义,最好在主窗体中设置,不要忘记enctype="multipart/form-data"参数<form>:
<body>
<form enctype="multipart/form-data" id="form1" runat="server">
<input type='file' id='uploadFieldNameFromHTML' />
...

