1

我正在使用 ajax 表单将多个文件上传到服务器。这就是我的表格的样子

   @using (@Ajax.BeginForm("SaveProjectSummary", "Project", new { id = Model.ProjectId }, new AjaxOptions { OnSuccess = "Add_OnCompleteSummarySave()" }, new { enctype = "multipart/form-data" }))

我可以引导我采取我想要的行动,但我在服务器上的 Request.Files 中找不到任何东西。只是想确定我是否使用了这个对我来说似乎没有的权利,所以任何帮助都将不胜感激。

4

4 回答 4

2

另一个很棒的上传器组件是 PlUpload

http://www.plupload.com/

它确实:

  • 夹住文件上传器,因此您可以上传大文件。例如 100Mb
  • 客户端文件缩放
  • 拖放文件选择

仅供参考,.NET 中用于处理来自 PlUpload 的发布请求的服务器端代码:

var chunk = Request.Form["chunk"] != null ? int.Parse(Request.Form["chunk"]) : 0;

var fileName = Request.Form["name"] ?? "";

//open a file, if our chunk is 1 or more, we should be appending to an existing file, otherwise create a new file
var fs = new FileStream(Server.MapPath("/files/" + fileName), chunk == 0 ? FileMode.OpenOrCreate : FileMode.Append);

//write our input stream to a buffer
var buffer = new Byte[Request.Files[0].InputStream.Length];
Request.Files[0].InputStream.Read(buffer, 0, buffer.Length);

//write the buffer to a file.
fs.Write(buffer, 0, buffer.Length);
fs.Close();

如果您热衷于推出自己的组件,请查看现代浏览器上可用的文件系统 API。它允许您获取文件内容二进制文件并使用 AJAX 调用上传

于 2013-02-21T05:10:40.043 回答
2

遗憾的是,Ajax.BeginForm 不能用于上传文件。

  • 有像uploadify这样的插件

http://www.uploadify.com/forum/#/discussion/1685/uploadify-and-ajax/p1

  • 或者这个 jquery 插件

http://malsup.com/jquery/form/

于 2013-02-21T04:53:24.700 回答
0

[HttpPost] public JsonResult UploadImage(HttpPostedFileWrapper imageFile) {

        if (imageFile == null || imageFile.ContentLength == 0 || imageFile.ContentLength > 205168)
        {

            return new JsonResult
            {
                ContentType = "text/html",
                Data = "No file was uploaded."

            };
        }

        if (imageFile == null || imageFile.ContentLength == 0 || (imageFile.ContentType != "image/png" && imageFile.ContentType != "image/jpg" && imageFile.ContentType != "image/jpeg" && imageFile.ContentType != "image/pjpeg"))
        {
            return new JsonResult
            {
                ContentType = "text/html",
                Data = "Type"
            };
        }
        if (Session["CityId"] != null)
        {
            if (MdlNadal == null)
            {
                MdlNadal = new mdlNadal();
            }
            string strFilePaths = "";

            int CityId = Convert.ToInt32(Session["CityId"].ToString());
            string strCityName = "";
            if (Session["CityName"] != null)
            {
                strCityName = Session["CityName"].ToString();
            }

            string strFileNames = imageFile.FileName.Replace(@"\", "/");
            string imgPath = ConfigurationManager.AppSettings["ImagePath"].ToString().Replace("~", "");
            strFileNames = strFileNames.Split('/')[strFileNames.Split('/').Length - 1];
            Session["ImageName"] = strFileNames;
            ViewBag.ImageName = strFileNames;
            strFilePaths = Request.Url.Scheme + "://" + Request.Url.Authority + imgPath + strCityName + "" + CityId + "/" + strFileNames;
            MdlNadal.UpdateCityImageByCityID(CityId, strFilePaths);
            if (imageFile != null)
            {
                if (!Directory.Exists(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId))))
                {
                    Directory.CreateDirectory(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId)));
                }
                else
                {
                    int counts = Directory.GetFiles(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId))).Count();
                    if (counts > 1)
                    {
                        string[] StrArr = Directory.GetFiles(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId)));
                        for (int i = 0; i <= counts - 1; i++)
                        {
                            string strFileNameCheck = StrArr[i];
                            //strFileNameCheck = strFileNameCheck.Replace(@"\", "/");
                            //strFileNameCheck = strFileNameCheck.Split('/')[strFileNameCheck.Split('/').Length - 1];
                            try
                            {
                                System.IO.File.Delete(strFileNameCheck);
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }

                        }
                    }
                }
                var FilePath = Path.Combine(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId)), strFileNames);
                imageFile.SaveAs(FilePath);
            }

        }

        return new JsonResult
        {
            ContentType = "text/html",
            Data = "Save"

        };
    }

@using (Html.BeginForm("UploadImage", "Nadal", FormMethod.Post, new { enctype = "multipart/form-data", id = "mws-Validate", target = "UploadTarget", @class = "mws -形式” })) {

                    <label class="title10">
                        <strong>Choose a background for this city</strong>
                    </label>                                             
                    <div class="mws-form-item large">
                        <input type="file" name="imageFile" id="imageFile" onchange="return SetAttachFlag();" />
                        (Supported File Types : .jpg, .jpeg, .png) (Maximum Size: 200 Kb)
                        <iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute;
                            left: -999em; top: -999em;"></iframe>
                    </div>

                }
于 2013-02-21T06:42:06.620 回答
0

为什么不利用 HTML5 中的 File API。查看这些链接,为您指明正确的方向。

http://www.html5rocks.com/en/tutorials/file/dndfiles/

http://timothypoon.com/blog/2011/05/10/ajax-uploading-with-html5s-file-api/

于 2013-02-21T11:00:56.553 回答