MVC3 Valums Ajaxfile upload

我正在尝试使用valums ajax上传器。 http://valums.com/ajax-upload/

我的页面上有以下内容:

var button = $('#fileUpload')[0]; var uploader = new qq.FileUploader({ element: button, allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], sizeLimit: 2147483647, // max size action: '/Admin/Home/Upload', multiple: false }); 

它发布到我的控制器,但qqfile始终为空。 我试过这些:

 public ActionResult Upload(HttpPostedFile qqfile) AND HttpPostedFileBase file = Request.Files["file"]; 

没有任何运气。

我发现在轨道上的ruby的例子,但不知道如何在MVC中实现它http://www.jigsawboys.com/2010/10/06/ruby-on-rails-ajax-file-upload-with-valum/

在萤火虫中,我看到: http:// localhost:61143 / Admin / Home / Upload?qqfile = 2glonglonglongname + – + Copy.gif

在这里输入图像说明

在这里输入图像说明在这里输入图像说明

在这里输入图像说明

我想到了。 这在IE和Mozilla的作品。

  [HttpPost] public ActionResult FileUpload(string qqfile) { var path = @"C:\\Temp\\100\\"; var file = string.Empty; try { var stream = Request.InputStream; if (String.IsNullOrEmpty(Request["qqfile"])) { // IE HttpPostedFileBase postedFile = Request.Files[0]; stream = postedFile.InputStream; file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName)); } else { //Webkit, Mozilla file = Path.Combine(path, qqfile); } var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); System.IO.File.WriteAllBytes(file, buffer); } catch (Exception ex) { return Json(new { success = false, message = ex.Message }, "application/json"); } return Json(new { success = true }, "text/html"); } 

该组件正在发送一个application/octet-stream而不是multipart/form-data ,这是默认模型联编程序可以使用的。 所以你不能期望Request.Files对这样的请求有任何价值。

您将需要手动读取请求stream:

 public ActionResult Upload(string qqfile) { var stream = Request.InputStream; var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); var path = Server.MapPath("~/App_Data"); var file = Path.Combine(path, qqfile); File.WriteAllBytes(file, buffer); // TODO: Return whatever the upload control expects as response } 

IE使用multipart-mime上传。 其他浏览器使用Octet-Stream。

我写了一个上传处理程序与Valums Ajax Uploader一起使用,可以同时使用MVC和Webforms两种上传方法。 如果您愿意,我很乐意与您分享。 它紧跟在PHP处理程序之后。

我的控制器来处理上传看起来像这样:

 public class UploadController : Controller { private IUploadService _Service; public UploadController() : this(null) { } public UploadController(IUploadService service) { _Service = service ?? new UploadService(); } public ActionResult File() { return Content(_Service.Upload().ToString()); } 

UploadService看起来如下所示:

 public class UploadService : IUploadService { private readonly qq.FileUploader _Uploader; public UploadService() : this(null) { } public UploadService(IAccountService accountservice) { _Uploader = new qq.FileUploader(); } public UploadResult Upload() { qq.UploadResult result = _Uploader.HandleUpload(); if (!result.Success) return new UploadResult(result.Error); .... code ..... return new UploadResult((Guid)cmd.Parameters["@id"].Value); } catch (Exception ex) { return new UploadResult(System.Web.HttpUtility.HtmlEncode(ex.Message)); } finally { ............code......... } } ...............code ............ 

你应该试试:

 Stream inputStream = (context.Request.Files.Count > 0) ? context.Request.Files[0].InputStream : context.Request.InputStream; 

我正在开发ASP.Net 4.0,但我们没有MVC架构。 我几天前也有同样的问题。 但是,我明白了,这是我的解决scheme。

 //For IE Browser HttpPostedFile selectedfile = Request.Files[0]; System.Drawing.Bitmap obj = new System.Drawing.Bitmap(selectedfile.InputStream); //For Non IE Browser System.Drawing.Bitmap obj = new System.Drawing.Bitmap(Request.InputStream); 

现在,您可以使用obj进一步操作。