gpt4 book ai didi

asp.net-mvc - 在asp.net mvc中使用fileupload时如何防止IE保存文件对话框

转载 作者:行者123 更新时间:2023-12-04 22:15:14 26 4
gpt4 key购买 nike

当我尝试使用 ASP.NET MVC 上传文件时,它在 FF && Chrome 中工作正常,但在 IE 和 Opera 中会弹出一个对话框,要求我下载、保存或取消。

选择任何一个选项,都会阻止文件上传工作。我怎样才能解决这个问题?

 public class ImportModel
{
[Required]
[FileExtensions("csv", ErrorMessage = "Please upload a valid .csv file")]
public HttpPostedFileBase File { get; set; }
}



[HttpPost]
public virtual ActionResult StartImport(ImportModel model = null)
{
if (model != null)
{
var status = _importService.StartImport(model);
return Json(status, JsonRequestBehavior.AllowGet);
}
return null;
}

查看下面的代码(总结):
<div id="dlgImport" class="hidden">

@using (Html.BeginForm(MVC.Import.StartImport(), FormMethod.Post, new { @class = "smallForm", id = "frmImport", enctype = "multipart/form-data" }))
{
<div class="fields-inline">
<div class="editor-label">
@Html.Label("File")
</div>
<div class="editor-field">
@Html.TextBoxFor(x => x.File, new { @class="input-file", type = "file" })
@Html.ValidationMessageFor(x => x.File)
</div>
</div>
}
</div>

</div>



$(function() {
$("#frmImport").ajaxForm({
success: function (responseHtml) {
// response is wrapped in pre tags by the browser - the ajax upload is carried out using an iframe
var response = $.parseJSON($(responseHtml).text());
}
});
});


var vm = {

startImport: function () {

if ($("#frmImport").valid()) {
$("#frmImport").submit();
}
}

}

最佳答案

现在您已经发布了代码,看起来您正在使用 jquery form plugin 。如 documentation 中所述,此插件支持使用 AJAX 上传文件,但您无法从服务器端脚本返回 JSON:

Since it is not possible to upload files using the browser's XMLHttpRequest object, the Form Plugin uses a hidden iframe element to help with the task. This is a common technique, but it has inherent limitations. The iframe element is used as the target of the form's submit operation which means that the server response is written to the iframe. This is fine if the response type is HTML or XML, but doesn't work as well if the response type is script or JSON, both of which often contain characters that need to be repesented using entity references when found in HTML markup.

To account for the challenges of script and JSON responses, the Form Plugin allows these responses to be embedded in a textarea element and it is recommended that you do so for these response types when used in conjuction with file uploads. Please note, however, that if there is no file input in the form then the request uses normal XHR to submit the form (not an iframe). This puts the burden on your server code to know when to use a textarea and when not to.



所以基本上你的上传 Controller Action 应该响应:
<textarea>{"foo":"bar"}</textarea>

代替:
{"foo":"bar"}

在这种情况下,您也不应该使用 application/json 响应内容类型。

您可以编写自定义操作结果来实现:
public class FileJsonResult : JsonResult
{
public FileJsonResult(object data)
: base()
{
Data = data;
JsonRequestBehavior = JsonRequestBehavior.AllowGet;
}

public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Write("<textarea>");
base.ExecuteResult(context);
context.HttpContext.Response.Write("</textarea>");
context.HttpContext.Response.ContentType = "text/html";
}
}

然后:
[HttpPost]
public virtual ActionResult StartImport(ImportModel model = null)
{
if (model != null)
{
var status = _importService.StartImport(model);
return new FileJsonResult(status);
}
new FileJsonResult(new { status = false, errorMessage = "The model was null" });
}

现在您可能还需要调整成功处理程序以去除 <textarea> 标签:
$('#frmImport').ajaxForm({
success: function (responseHtml) {
var responseHtml = responseHtml
.replace(/\<textarea\>/i, '')
.replace(/\<\/textarea\>/i, '');
var response = $.parseJSON(responseHtml);
// do something with the response
}
});

关于asp.net-mvc - 在asp.net mvc中使用fileupload时如何防止IE保存文件对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9417977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com