2012-02-09 72 views
2

我正在使用Blueimp提供的jQuery文件上传版本5.6提供的演示页面。我可以在我的ASP.NET MVC项目中运行演示,以便可以从页面上载文件。从jQuery File Upload插件的控制器动作返回什么?

但是,即使文件成功上传,UI也会报告错误。我100%肯定这个错误是因为我没有从我的控制器动作中返回适当的信息。

这里是我现有的行动:

[HttpPost] 
    public virtual JsonResult ImageUpload(FormCollection formdata) 
    { 
     var imagePath = Setting.Load(SettingKey.BlogImagesBasePath).Value; 
     for(var i = 0; i < Request.Files.Count; i++) 
     { 
      var saveFileName = string.Format("{0}\\{1}", imagePath, Request.Files[i].FileName); 
      Log.DebugFormat("Attempting to save file: {0}", saveFileName); 
      Request.Files[i].SaveAs(saveFileName); 
     } 

     return Json(new {}); 
    } 

我不知道结果的内容应该是什么。我尝试通过php示例进行排序,但对php完全不熟悉,我可以做的最好的是可能涉及到文件名,大小和类型。

是否有人有一个工作MVC示例的链接或提供我需要的信息将正确的数据返回给插件?

回答

1

帆船柔道

我认为这个问题解决您的需求100%:

jQuery File Upload plugin asks me to download the file, what is wrong?

这里基本上什么演示:

类:

public class ViewDataUploadFilesResult 
{ 
    public string Name { get; set; } 
    public int Length { get; set; } 
    public string Type { get; set; } 
} 

的行动:

[HttpPost] 
public JsonResult UploadFiles() 
{ 
    var r = new List<ViewDataUploadFilesResult>(); 
    Core.Settings settings = new Core.Settings(); 
    foreach (string file in Request.Files) 
    { 
     HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase; 
     if (hpf.ContentLength == 0) 
      continue; 
     string savedFileName = Path.Combine(settings.StorageLocation + "\\Files\\", Path.GetFileName(hpf.FileName)); 
     hpf.SaveAs(savedFileName); 

     r.Add(new ViewDataUploadFilesResult() 
     { 
      Name = hpf.FileName, 
      Length = hpf.ContentLength, 
      Type = hpf.ContentType 
     }); 
    } 
    return Json(r); 
} 

所以,基本上,你只需要返回ViewDataUploadFilesResult集合的jsonresult。

希望它有帮助。

+0

它的确如此,谢谢。我搜索了很多次,但没有看到这个。 – 2012-02-09 15:06:05

+0

sj - 我确切地知道你的意思,我是那种在寻找它们时将他的眼镜留在头顶上的人:) – 2012-02-09 15:10:34

+0

使用上面的代码,我收到一个错误:“空文件上传结果”该响应不是一个文件[]数组。我在一个不相关的示例中发现了以下内容并对其进行了修改,希望此添加可以帮助任何遇到相同问题的人:var uploadedFiles = new { files = r.ToArray() }; return Json(uploadedFiles); – 2014-06-02 21:28:35

相关问题