简体   繁体   English

上载多个档案aspx.net C#

[英]Upload more than one file aspx.net C#

I have the following HTML source: 我有以下HTML来源:

 <form name="AddTrack" id="add_track_form" action="AddTrack.aspx" method="post" runat="server">


       <input type="file" name="file1"/><br />
            <input type="file" style="margin-right: 52px;" name="file2" /><br />
            <input type="file" style="margin-right: 52px;" name="file3" /><br />
            <input type="file" style="margin-right: 52px;" name="file4" /><br />
        <button type="submit" class="blue-button">הוסף מסלול</button>
    </form>

With this ASPX - C# code: 使用此ASPX-C#代码:

if (Request.ContentLength != 0)
{
    int Size = Request.Files[0].ContentLength / 1024;
    if (Size <= 512)
    {

        string LocalFile = Request.Files[0].FileName;
        int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
        string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
        string Path = Server.MapPath(" ../images/tracks") + "..\\" + File;
        Request.Files[0].SaveAs(Path);
        Response.Write(@"The file was saved: " + Path);
    }
    else
    {
        Response.Write("The file is too big !");
    }
}
else
{
    Response.Write("Unknown Error !");
}

If I upload one file it works great, but I upload there is more than one upload input it don't work. 如果我上传一个文件,效果很好,但是我上传的文件多于一个,则不起作用。

My question is why and how can I fix it? 我的问题是为什么以及如何解决?

As far as I can see, you just need to add enctype="multipart/form-data" to your form: 据我所知,您只需要在enctype="multipart/form-data"添加enctype="multipart/form-data"

 <form name="AddTrack" id="add_track_form" action="AddTrack.aspx" method="post" runat="server" enctype="multipart/form-data">

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2 http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. 内容类型“ application / x-www-form-urlencoded”对于发送大量二进制数据或包含非ASCII字符的文本效率不高。 The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data. 内容类型“ multipart / form-data”应用于提交包含文件,非ASCII数据和二进制数据的表单。

You are not using asp:FileUpload control which adds that enctype automatically, so you should do that manually. 您没有使用asp:FileUpload控件,该控件会自动添加该enctype,因此您应该手动执行此操作。

for(int i = 0; i < Request.Files.Count; i++) {

    int Size = Request.Files[i].ContentLength / 1024;
    if (Size <= 512)
    {
       string LocalFile = Request.Files[i].FileName;
    //.....
}

I suggest you to use this uploadify library , it's free 我建议您使用这个uploadify library ,它是免费的

Because basic Upload file asp.net don't offer possibility of multiple downloading 因为基本的上传文件asp.net不提供多次下载的可能性

link : http://www.uploadify.com/download/ 链接: http//www.uploadify.com/download/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM