在MVC3中循环uploadify上传的已发布文件

发布于 2024-11-28 13:29:06 字数 1381 浏览 0 评论 0原文

我正在使用 uploadify 在 MVC 3 应用程序中上传多个文件。如果我选择多个文件并点击“上传”,我会在控制器中逐一获取这些文件。我想要的是发布的文件作为一个集合,以便我可以循环浏览它们。

我在这里看到了一些相关帖子,但我不能使用它,因为它不使用 uploadify。

知道怎么做吗?

Razor View 代码:

<input type="file" id="file_upload" name="file_upload" />
<a onclick="Upload();" style="font:12px Arial; text-decoration: none; outline: none;">Upload</a>
<input type="hidden" id="CanFiles" />

$('#file_upload').uploadify({
            'uploader': '/content/uploadify/uploadify.swf',
            'script': '/upload/UploadFiles',
            'scriptData': { 'auth': auth, 'sid': sid, 'multi': 1 },
            'cancelImg': '/content/uploadify/cancel.gif',
            'folder': '/content/uploadify/uploads',
            'auto': false,
            'multi': true,
            'simUploadLimit': 1,
            'expressInstall': '/content/uploadify/expressInstall.swf',
});

当前控制器操作:

public ActionResult UploadFiles()
{
    foreach (string fileName in Request.Files)
    {
       //this always get 1 file and action gets called once for each file
    }
}

这就是我想要的:

public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
{
    //loop the files posted
}

I am using uploadify to upload multiple files in an MVC 3 app. If I select multiple files and hit Upload, I get the files but one-by-one in the controller. What I want is that the posted files come as a collection so that I can loop through them.

I saw some related post here, but I can't use that since it doesn't use uploadify.

Any idea how to do it?

Razor View code:

<input type="file" id="file_upload" name="file_upload" />
<a onclick="Upload();" style="font:12px Arial; text-decoration: none; outline: none;">Upload</a>
<input type="hidden" id="CanFiles" />

$('#file_upload').uploadify({
            'uploader': '/content/uploadify/uploadify.swf',
            'script': '/upload/UploadFiles',
            'scriptData': { 'auth': auth, 'sid': sid, 'multi': 1 },
            'cancelImg': '/content/uploadify/cancel.gif',
            'folder': '/content/uploadify/uploads',
            'auto': false,
            'multi': true,
            'simUploadLimit': 1,
            'expressInstall': '/content/uploadify/expressInstall.swf',
});

current controller action:

public ActionResult UploadFiles()
{
    foreach (string fileName in Request.Files)
    {
       //this always get 1 file and action gets called once for each file
    }
}

This is what I want:

public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
{
    //loop the files posted
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2024-12-05 13:29:06

您需要的是对同一操作发布多个文件。为此,不需要使用像 uploadify 这样的库。您可以轻松地执行以下操作:编写 JavaScript 以在用户选择文件后立即隐藏文件元素,并在其位置创建一个新的文件元素。这样,您就有多个文件元素指向不同的文件,除了一个文件之外的所有文件元素都被隐藏,但显示在页面上,并且当用户提交表单时,您将获得 Request.Files 数组中的所有文件。我将编辑这个答案并很快给你一个样本。

编辑:

这是一个示例:

http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/

这有点基本,你可以让它工作。

What you need is a post of multiple files to the same action. In order to do that, it is not necessary to use a library like uploadify. What you can easily do is this : write javascript to hide a file element as soon as the user has chosen a file and create a new file element in its place. That way, you have multiple file elements pointing to different files, all except one is concealed, but present on the page, and when user submits the form, you get all files in the Request.Files array. I will edit this answer and give you a sample soon.

EDIT:

Here is an example :

http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/

It is a little bit basic, you can make it work though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文