jQuery File Upload 插件要求我下载文件,这是怎么回事? [英] jQuery File Upload plugin asks me to download the file, what is wrong?
问题描述
我正在使用 https://github.com/blueimp/jQuery-File-Upload 我能够将文件上传并保存到指定的文件夹,然后我返回 Json 对象.然后浏览器(我使用 IE8)弹出文件下载"对话框并要求我下载一个名为upload75bea5a4"且没有扩展名的文件.我就是想不通是哪里出了问题?
我使用的是同一个插件,它对我来说没有任何问题.我将发布我正在使用的代码,以便为您提供帮助.我在 Scott Hanselman 的博客 看到的 C# 代码(我做了一些改动).p>
用于存储文件属性的类:
公共类 ViewDataUploadFilesResult{公共字符串名称 { 获取;放;}公共 int 长度 { 获取;放;}公共字符串类型 { 获取;放;}}
上传代码,由ajax调用:
[HttpPost]公共字符串 UploadFiles(){var r = 新列表<ViewDataUploadFilesResult>();Core.Settings 设置 = new Core.Settings();foreach(Request.Files 中的字符串文件){HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;如果(hpf.ContentLength == 0)继续;string savedFileName = Path.Combine(settings.StorageLocation + "\Files\", Path.GetFileName(hpf.FileName));hpf.SaveAs(savedFileName);r.Add(新的 ViewDataUploadFilesResult(){名称 = hpf.FileName,长度 = hpf.ContentLength,类型 = hpf.ContentType});}返回 "{"name":"" + r[0].Name + "","type":"" + r[0].Type + "","size":"" + string.Format("{0} bytes", r[0].Length) + ""}";}
创造魔法的 javascript 片段:
$('#file_upload').fileUploadUI({上传表:$('#files'),下载表:$('#files'),buildUploadRow:函数(文件,索引){return $('<tr><td>' + 文件[索引].name + '</td>' +'<td class="file_upload_progress"><div></div></td>'+'<td class="file_upload_cancel">'+'<button class="ui-state-default ui-corner-all" title="取消">'+'<span class="ui-icon ui-icon-cancel">取消</span>'+'</button></td></tr>');},buildDownloadRow:函数(文件){return $('<tr><td>' + file.name + '</td></tr>');}});
看看并做一些测试.
--
编辑
I'm using https://github.com/blueimp/jQuery-File-Upload and I was able to upload and save the file to designated folder, and then I return the Json object. Then the browser (I use IE8) pops "File Download" dialog and asks me to download a file named "upload75bea5a4" with no extension. I just could not figure out what is wrong?
I am using the same plugin and it's working without any problems for me. I will post the codes I am using so that can help you. The C# code I saw at Scott Hanselman's blog (and I made few changes).
A class to store the properties of the file:
public class ViewDataUploadFilesResult
{
public string Name { get; set; }
public int Length { get; set; }
public string Type { get; set; }
}
The upload code, which is called by the ajax:
[HttpPost]
public string 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 "{"name":"" + r[0].Name + "","type":"" + r[0].Type + "","size":"" + string.Format("{0} bytes", r[0].Length) + ""}";
}
The javascript piece which makes the magic:
$('#file_upload').fileUploadUI({
uploadTable: $('#files'),
downloadTable: $('#files'),
buildUploadRow: function (files, index) {
return $('<tr><td>' + files[index].name + '</td>' +
'<td class="file_upload_progress"><div></div></td>' +
'<td class="file_upload_cancel">' +
'<button class="ui-state-default ui-corner-all" title="Cancel">' +
'<span class="ui-icon ui-icon-cancel">Cancel</span>' +
'</button></td></tr>');
},
buildDownloadRow: function (file) {
return $('<tr><td>' + file.name + '</td></tr>');
}
});
Take a look and make some tests.
--
EDIT
这篇关于jQuery File Upload 插件要求我下载文件,这是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!