简体   繁体   English

在C#中动态上传文件

[英]dynamic file upload in c#

I have absolutely no idea on how to upload multiple files in asp.net using c#,with single upload button.Its not known in advance ,how many files are there. 我绝对不知道如何使用单个上传按钮使用c#在asp.net中上传多个文件。它事先未知,有多少个文件。 Can somebody provide me the code in c#??I would be grateful. 有人可以为我提供c#中的代码吗?我将不胜感激。

Thanks in advance!! 提前致谢!!

Multiple uploads are not possible using a single upload control (you'll have to upload one file, then repeat the whole process again after the first file has been uploaded). 使用单个上传控件无法进行多次上传(您必须上传一个文件,然后在第一个文件上传后再次重复整个过程)。

You can use an IFrame & some JS to rig up one such control which will allow you to upload multiple files at once (But then also, only one file will be posted to the server at a time, and its for the better, for the server). 您可以使用IFrame和某些JS来建立一个这样的控件,该控件将允许您一次上传多个文件(但同时,一次只能将一个文件发布到服务器,对于服务器)。

Or you can use some third party controls created using Java technology (Applets) or in Flash. 或者,您可以使用通过Java技术(Applets)或在Flash中创建的某些第三方控件。

This is an example using multiple textboxes and browse buttons to collect the paths of up to 5 files and then uploads them at once. 此示例使用多个文本框和浏览按钮来收集最多5个文件的路径,然后一次上传它们。

DotNetJunkies File Upload Tutorial DotNetJunkies文件上传教程

This one from MSDN uses the File Field Control to accomplish the same thing. 来自MSDN的该文件使用文件字段控件来完成相同的操作。

There is a lot of code in both of those articles that should get you well on your way. 这两篇文章中都有很多代码,应该可以帮助您顺利完成工作。

You can create one upload input and have a button to add more dynamically using Javascript. 您可以创建一个上传输入,并有一个按钮,可以使用Javascript动态添加更多内容。 When you click the save button, the files will all be in Request.Files. 当您单击保存按钮时,文件将全部位于Request.Files中。

<script type="text/javascript">
    var uploadCount = 2;
    function AddUpload()
    {
        var uploads = document.getElementById("uploads");
        var id = "upload" + uploadCount;
        uploads.innerHTML += ("<input type='file' id='" + id + "' name='" + id + "' />");
    }
</script>

<a href="javascript: void(0);" onclick="javascript: AddUpload();">Add Upload</a>
<div id="uploads">
    <asp:FileUpload runat="server" ID="upload1" />
</div>
<asp:Button runat="server" ID="btnSave" Text="Save" />

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

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