创建多个文件来自多个文件上传

huangapple go评论61阅读模式
英文:

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.

&lt;asp:FileUpload ID=&quot;FileUpload1&quot; runat=&quot;server&quot; AllowMultiple=&quot;true&quot; /&gt;
&lt;asp:Button ID=&quot;Button1&quot; Text=&quot;Upload File&quot; runat=&quot;server&quot; OnClick=&quot;UploadFile&quot; /&gt;

&#39;--Upload Multiple Files
    Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim filePath As String = Server.MapPath(&quot;~/Files/&quot;) &amp; fileName
    FileUpload1.SaveAs(filePath)


    For Each postedFile As HttpPostedFile In FileUpload1.PostedFiles


       &#39;-- Create new file to output
        Dim NewFile As String = filePath &amp; &quot;_NewCreatedFile.txt&quot;
        Dim FilWtr As New StreamWriter(NewFile)


        &#39;-- 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(&quot;~/Files/&quot;) &amp; fileName

        FileUpload1.SaveAs(filePath)

    Next

huangapple
  • 本文由 发表于 2023年2月14日 19:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定