使用MVC3文件上传AJAX形式 - Request.Files空 [英] mvc3 file upload using ajax form - Request.Files empty

查看:140
本文介绍了使用MVC3文件上传AJAX形式 - Request.Files空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用im MVC3,并想创建一系列的每个上传的文件,在一个页面的ajax形式。下面是查看页:

Im using mvc3, and am trying to create a series of ajax forms that each upload a file, within a single page. Here's the view for the page:

@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>

    @Html.Partial("_UploadItem")
    @Html.Partial("_UploadItem")
    @Html.Partial("_UploadItem")

<script type="text/javascript">
    function Go() {
        // loop through form tags
        for (var n = 0; n < document.forms.length; n++) {
            var f = document.forms[n];
            // if a dress is chosen, a caption is chosen
            // and a file is chosen, then submit the ajax form
            if (f.dressid.value != '' &&
               f.dresscaption.value != '' &&
               f.fileitem.value != '')
                f.submit();
        }
    }
</script>
<input type="button" onclick="Go();"/>

然后转到()函数循环通过AJAX的形式,检查,看看所有的3件(dressid,dresscaption和的FileItem)每份表格上是否非空,并呼吁提出,做表格,开球异步上传

Then the Go() function loops through the ajax forms, checking to see whether all 3 pieces (dressid, dresscaption, and fileitem) on each form are non-empty, and calls submits the forms that do, kicking off an async upload.

下面的部分观点:

@using SoRefeising.Models
@using (Ajax.BeginForm("UploadFile", new { }, new AjaxOptions { HttpMethod = "POST" }, new { enctype="multipart/form-data"}))
{
    List<SelectListItem> items = (List<SelectListItem>)ViewBag.Dresses;

    <span>Dress</span>
    @Html.DropDownList("dressid", items, "Choose Dress");
    <span>Caption</span>
    @Html.TextBox("dresscaption")
    <input type="file" id="fileitem" />    
}

我已经打上各种形式的多部分属性。当生成的页面,我得到如下:

I have marked each form with the multipart attribute. When the page is generated, I get the following:

<form action="/upload/UploadFile" data-ajax="true" data-ajax-method="POST" enctype="multipart/form-data" id="form0" method="post">    <span>Dress</span>
<select id="dressid" name="dressid"><option value="">Choose Dress</option>
<option value="1">Simpson01</option>
<option value="2">Simpson02</option>
</select>    <span>Caption</span>
<input id="dresscaption" name="dresscaption" type="text" value="" />    <input type="file" id="fileitem" />    
</form>

    <form action="/upload/UploadFile" data-ajax="true" data-ajax-method="POST" enctype="multipart/form-data" id="form1" method="post">    <span>Dress</span>
<select id="dressid" name="dressid"><option value="">Choose Dress</option>
<option value="1">Simpson01</option>
<option value="2">Simpson02</option>
</select>    <span>Caption</span>
<input id="dresscaption" name="dresscaption" type="text" value="" />    <input type="file" id="fileitem" />    
</form>

    <form action="/upload/UploadFile" data-ajax="true" data-ajax-method="POST" enctype="multipart/form-data" id="form2" method="post">    <span>Dress</span>
<select id="dressid" name="dressid"><option value="">Choose Dress</option>
<option value="1">Simpson01</option>
<option value="2">Simpson02</option>
</select>    <span>Caption</span>
<input id="dresscaption" name="dresscaption" type="text" value="" />    <input type="file" id="fileitem" />    
</form>

一切看起来都OK了...

Everything looks ok...

下面是被称为控制器动作

Here's the controller action that is called

[HttpPost]
        public ActionResult UploadFile(string dressid, string dresscaption)
        {
                HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
...

在动作被调用时,Request.Files收藏有0项,而不是选定的文件。我已经启用非侵入式JavaScript,不显眼的文件被加载在母版页,以及适用于其他网页。

When the action is called, the Request.Files collection has 0 items, rather than the selected file. I have enabled unobtrusive javascript, the unobtrusive file is loaded in the master page, and works on other pages.

我见过的约小心与文件大小论坛的一些消息。我用来测试的文件为&lt; 2K

Ive seen some messages on the forum about being careful with file sizes. The file I am testing with is < 2k

任何想法,为什么在Request.Files没有任何项目?

Any ideas why there are no items in Request.Files?

感谢

推荐答案

您无法上传使用AJAX的文件。因此,与普通的 Html.BeginForm 替换 Ajax.BeginForm 。您可以检出以下博客帖子为好。

You cannot upload files using AJAX. So replace the Ajax.BeginForm with a normal Html.BeginForm. You may checkout the following blog post as well.

如果你想使用异步上传,你可以尝试一些可用的上传组件,如阿贾克斯上传和的 Uploadify

If you want to use asynchronous uploads you may try some of the available upload components such as Ajax Upload and Uploadify.

这篇关于使用MVC3文件上传AJAX形式 - Request.Files空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆