【问题标题】:Httppostedfilebase always returns null when posted with jquery ajax使用 jquery ajax 发布时,Httppostedfilebase 始终返回 null
【发布时间】:2020-01-14 23:29:51
【问题描述】:

我正在开发一个 mvc 应用程序并希望将文件上传到服务器。 在控制器上,当事件触发时,fileupload 为 null。而且 model.fileupload 也有路径。 实际上是使用 ajax post 方法提交我的表单。

cshtml代码....

<form id="frmRegistrationDetails" method="post" enctype="multipart/form-data">
    <table>
      <tr>
         <td>
            <label>Upload File</label><input type="file" id="fileupload"  name="fileupload" />
         </td>
      </tr>
   </table>
</form>

控制器代码

 [HttpPost]
 public JsonResult SaveDetails(reg_up_mvc model,HttpPostedFileBase fileupload)
  {
  try
  {
    detailsRegistration registrationContext = new detailsRegistration();
    reg_up_mvc registrationDetails = new reg_up_mvc();
    registrationDetails.full_name = model.full_name;
    registrationDetails.fileupload = model.fileupload;
if (model.fileupload != null)
{
string sPath = Path.Combine(Server.MapPath("~/UploadedFilesFromRegistration"), fileupload.FileName);
fileupload.SaveAs(sPath);
registrationDetails.fileupload = sPath;
}

jquery

var data = {
        model: {
                fileupload: $('#fileupload').val()
               }
    };
     $.ajax({
        type: "post",
        data: JSON.stringify(data),
        url: "/Controller/ActionName",
        contentType: "application/json;charset=utf-8",
        success: function (response) {
                           alert("successful")
                           }
         });

【问题讨论】:

  • console.logJSON.stringify(data), 告诉我们它是什么。
  • model:fileupload:"C\fakepath\download.jpg".forgot to add id to
  • 曾尝试过使其相同但无能为力。所以只是更改了它的名称字段并尝试按名称元素检索值。但仍然没有希望
  • model:fileupload:"C\fakepath\download.jpg" 这看起来不像 JSON。你确定正是吗?
  • ,"fileupload":"C:\\fakepath\\download-hill.jfif" 那不是文件 contents。那是文件的路径。服务器对此无能为力。

标签: c# jquery ajax asp.net-mvc asp.net-ajax


【解决方案1】:
https://stackoverflow.com/questions/28373894/httppostedfilebase-is-null-using-jquery-ajax?noredirect=1&lq=1

已在此回答,请检查

【讨论】:

    猜你喜欢
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2017-09-24
    • 1970-01-01
    • 2016-05-31
    • 2015-08-15
    • 2012-03-18
    相关资源
    最近更新 更多