英文:
Create multiple files from a multiple file upload
问题
我可以使用FileUpload上传多个文件,但当我运行我的代码时,只创建一个新文件,尽管已成功上传多个文件。所以我的输出只有一个文件,尽管已成功上传多个文件。
有关如何修复这个问题的任何帮助将非常有帮助。提前感谢。
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<asp:Button ID="Button1" Text="Upload File" runat="server" OnClick="UploadFile" />
'--上传多个文件
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim filePath As String = Server.MapPath("~/Files/") & fileName
FileUpload1.SaveAs(filePath)
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
'-- 创建新文件以输出
Dim NewFile As String = filePath & "_NewCreatedFile.txt"
Dim FilWtr As New StreamWriter(NewFile)
'-- 关闭文件
FilWtr.Close()
Next
英文:
I can upload multiple files using FileUpload but when I run my code only ONE new file is created even though multiple are uploaded. So my output is only one file despite multiple being successfully uploaded.
Any help with how to fix this would be great. thanks in advance.
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<asp:Button ID="Button1" Text="Upload File" runat="server" OnClick="UploadFile" />
'--Upload Multiple Files
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim filePath As String = Server.MapPath("~/Files/") & fileName
FileUpload1.SaveAs(filePath)
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
'-- Create new file to output
Dim NewFile As String = filePath & "_NewCreatedFile.txt"
Dim FilWtr As New StreamWriter(NewFile)
'-- close file
FilWtr.Close()
Next
答案1
得分: 1
如果您允许多个文件,则上传控件会返回一个“这些文件的集合”。
这应该有效:
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
Dim fileName As String = Path.GetFileName(postedFile.FileName)
Dim filePath As String = Server.MapPath("~/Files/") & fileName
FileUpload1.SaveAs(filePath)
Next
英文:
If you allow multiple files, then the upload control does return a "collection of those files.
This should work:
For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles
Dim fileName As String = Path.GetFileName(postedFile.FileName)
Dim filePath As String = Server.MapPath("~/Files/") & fileName
FileUpload1.SaveAs(filePath)
Next
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论