上传是将jquery.unobtrusive-ajax.js参考后空上传、jquery、unobtrusive、ajax

2023-09-10 14:33:40 作者:撒一口袋喜欢

如果我不是指 jquery.unobtrusive-ajax.js 我可以在帖子的附件。如果我指的是它给了我零。

If I don't refer jquery.unobtrusive-ajax.js I can get attachment on Post. If I refer it It's giving me null.

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>


@using (Ajax.BeginForm("Index", "ContactSubmission", new AjaxOptions{ InsertionMode = InsertionMode.Replace, HttpMethod = "POST", OnSuccess = "updateSuccess" },
     new { enctype = "multipart/form-data",@class = "form-horizontal", role = "form" }))
      {
               ///code here

}

[HttpPost]
public JsonResult Index(Contact contact)
{
    if (ModelState.IsValid)
    {
       if (contact != null)
       {
         string attachment = string.Empty;
         // HttpPostedFileBase Attachment
         if (contact.Attachment != null) attachment = SaveFile(contact.Attachment); 
                ......

如何来处理呢?

How to handle this?

推荐答案

如果您不引用 jquery.unobtrusive-ajax.js ,你没有得到的 AJAX 的形式,而是一个普通的HTML表单。如果你这样做,我想表单工作正常,但不能上载与它的文件,如AJAX不允许的multipart / form-data的加密类型。

If you don't refer jquery.unobtrusive-ajax.js, you don't get the ajax form, but a regular HTML form. And if you do, I suppose the form works fine, but it is not possible to upload a file with it, as ajax does not allow multipart/form-data enctype.

您可以使用HTML 5文件API(使用文件从Web应用程序)或上传的jQuery插件。

You can use HTML 5 File API (Using files from web applications) or jQuery upload plugins.