我使用jQuery Ajax的形式上传文件,这在Chrome和Firefox的效果很好,但它不工作在IE。 它弹出窗口一个窗口,告诉我保存,我要上传的文件。
我的代码的例子,如果必要的话,是这里的仪式:HTML:
<div class="addNewDocumentContent">
<form id="AddNewDocForm" action="@Url.Action("AddNewDocument", "BidForm")" enctype="multipart/form-data" method="post">
<div>
<input name="File" type="file" style="width: 80%;" />
</div>
<div>
<label>
@Labels.Name</label>
<input type="text" name="Name" style="width: 80%;" />
</div>
<div style="text-align: right;">
<button type="button" name="Back" value="Back">
@Buttons.GoBack
</button>
<button type="submit" name="Add" value="Back">
@Buttons.Add
</button>
</div>
</form>
JS:
//Document Ready=============================================================================
$(function () {
$('#AddNewDocForm').ajaxForm({
type: 'POST',
beforeSubmit: function () {
return $("#AddNewDocForm").valid();
},
success: function (documents) {
FillDocuments(documents);
$('#dialogAddNewDocument').dialog('close');
}
});
});
//Validate====================================================================================
//Validation=====================================================================================
$(function () {
$("#AddNewDocForm").validate({
ignore: ":not(:visible)",
rules: {
File: "required",
Name: "required"
}
});
});
//=========================================================================================
行动
[HttpPost]
public JsonResult AddNewDocument(DocumentModel document)
{
if (ModelState.IsValid)
{
List<DocumentModel> documents = null;
if (Session["Documents"] != null)
{
documents = (List<DocumentModel>)Session["Documents"];
var doc = documents.OrderByDescending(x => x.Number).Take(1).FirstOrDefault();
document.Number = doc != null ? doc.Number + 1 : 1;
document.FileName = document.File != null ? document.File.FileName : document.FileName;
documents.Add(document);
}
else
{
documents = new List<DocumentModel>();
document.Number = 1;
document.FileName = document.File != null ? document.File.FileName : document.FileName;
documents.Add(document);
Session["Documents"] = documents;
}
var displaydocs = documents.Select(x => new
{
Name = x.Name,
Number = x.Number,
File = x.File != null ? x.File.FileName : x.FileName,
Route = x.Route != null ? x.Route : "#",
});
return Json(displaydocs, JsonRequestBehavior.AllowGet);
}
else
{
return null;
}
}
最后模型:
public class DocumentModel
{
public int Number { get; set; }
[Required]
public string Name { get; set; }
[Required]
public HttpPostedFileBase File { get; set; }
public string FileName { get; set; }
public string Route { get; set; }
}
再次,它工作在IE8除了每一个浏览器。 林可能不是唯一的一个,但我还没有找到一个答案在那里。