jquery-plugins – jQuery文件上传插件要求我下载文件,有什么问题?

前端之家收集整理的这篇文章主要介绍了jquery-plugins – jQuery文件上传插件要求我下载文件,有什么问题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 https://github.com/blueimp/jQuery-File-Upload,我能够将文件上传并保存到指定的文件夹,然后我返回Json对象.然后浏览器(我使用IE8)弹出“文件下载”对话框,并要求我下载一个名为“upload75bea5a4”的文件,没有扩展名.我只是弄清楚出了什么问题?

解决方法

我正在使用相同的插件,它对我没有任何问题.我将发布我正在使用的代码,以便可以帮助您.我在 Scott Hanselman’s blog看到的C#代码(我做了一些改动).

用于存储文件属性的类:

  1. public class ViewDataUploadFilesResult
  2. {
  3. public string Name { get; set; }
  4. public int Length { get; set; }
  5. public string Type { get; set; }
  6. }

上传代码,由ajax调用

  1. [HttpPost]
  2. public string UploadFiles()
  3. {
  4. var r = new List<ViewDataUploadFilesResult>();
  5. Core.Settings settings = new Core.Settings();
  6. foreach (string file in Request.Files)
  7. {
  8. HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
  9. if (hpf.ContentLength == 0)
  10. continue;
  11. string savedFileName = Path.Combine(settings.StorageLocation + "\\Files\\",Path.GetFileName(hpf.FileName));
  12. hpf.SaveAs(savedFileName);
  13.  
  14. r.Add(new ViewDataUploadFilesResult()
  15. {
  16. Name = hpf.FileName,Length = hpf.ContentLength,Type = hpf.ContentType
  17. });
  18. }
  19. return "{\"name\":\"" + r[0].Name + "\",\"type\":\"" + r[0].Type + "\",\"size\":\"" + string.Format("{0} bytes",r[0].Length) + "\"}";
  20. }

制作魔法的javascript片段:

  1. $('#file_upload').fileUploadUI({
  2. uploadTable: $('#files'),downloadTable: $('#files'),buildUploadRow: function (files,index) {
  3. return $('<tr><td>' + files[index].name + '<\/td>' +
  4. '<td class="file_upload_progress"><div><\/div><\/td>' +
  5. '<td class="file_upload_cancel">' +
  6. '<button class="ui-state-default ui-corner-all" title="Cancel">' +
  7. '<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
  8. '<\/button><\/td><\/tr>');
  9. },buildDownloadRow: function (file) {
  10. return $('<tr><td>' + file.name + '<\/td><\/tr>');
  11. }
  12. });

看看并做一些测试.

编辑

I wrote an article about it.

猜你在找的jQuery相关文章