2016-11-18 20 views
0

試圖將文件和一些數據發佈到MVC 5後端。MVC5 404錯誤在文件和輸入後Angularjs

問題是它沒有正確映射,因此它返回了404。Http post請求正在作爲多部分/表單數據內容類型發送。

下面是從角服務

requestInputHeat: function (qty, date, camp, note, file1) { 
     return $http({ 
      method: 'POST', 
      url: '/log/heat/request', 
      headers: { 
       'Content-Type': 'multipart/form-data' 
      }, 
      data: { 
       Quantity : qty, 
       RequestDate: date, 
       CampaignDetail: camp, 
       Notes: note, 
       File: file1 
      }, 
      transformRequest: function (data, headersGetter) { 
       var formData = new FormData(); 
       angular.forEach(data, function (value, key) { 
        formData.append(key, value); 
       }); 

       var headers = headersGetter(); 
       delete headers['Content-Type']; 

       return formData; 
      } 
     }) 
    } 

在這裏,HTTP POST是在控制器MVC5後端我已經試圖接收此請求(使用mvcmapping atrributes)

[HttpPost] 
    [Route("log/heat/request")] 
    public ActionResult RequestPOHeat(string Quantity, string RequestDate, string CampaignDetail, string Notes, HttpPostedFileBase File) 
    { 
    ...... 
    } 

enter image description here

+0

請試試這個[Route(「〜/ log/heat/request」)]而不是[Route(「log/heat/request」)]。讓我知道它是否有效。 –

+0

不,我相信問題是它沒有在actionresult方法中看到正確的參數,所以它不會將它映射到該請求。由於參數確實匹配,我不確定如何使其映射。 –

回答

0

請請嘗試關注

In ang ularJs

function requestInputHeat(qty, date, camp, note, file1) { 
      var request = { 
       method: 'POST', 
     //public ActionResult RequestPOHeat(string Quantity, string RequestDate, string CampaignDetail, string Notes) 
     //your Controller Action RequestPOHeat requires 4 query strings which was previously not send in your code which I had passed below. This was giving 404 error. 
       url: '/log/heat/request?Quantity=' + qty.toString() + '&RequestDate=' + date.toString() + '&CampaignDetail=' + camp.toString() + '&Notes' + note.toString(),// I don't know the datatype so had converted them to string.Please change them to corresponding type once it's working. Also don't forget to map type with RequestPOHeat method parameter of controller. 
       data: { file: file1 }, 
       headers: { 
       //IMPORTANT!!! You might think this should be set to 'multipart/form-data' 
     // but this is not true because when we are sending up files the request 
     // needs to include a 'boundary' parameter which identifies the boundary 
     // name between parts in this multi-part request and setting the Content-type 
     // manually will not set this boundary parameter. For whatever reason, 
     // setting the Content-type to 'false' will force the request to automatically 
     // populate the headers properly including the boundary parameter. 
        'Content-Type': undefined 
       }, 
       transformRequest: function (data) { 
        var formData = new FormData(); 
        angular.forEach(data, function (value, key) { 
         formData.append(key, value); 
        }); 

        return formData; 
       }, 
      }; 


      return $http(request).success(function (result) { 
       return result.data; 
      }).error(function() { 

      }); 
     } 

在MVC控制器

[HttpPost] 
[Route("log/heat/request")] 
public ActionResult RequestPOHeat(string Quantity, string RequestDate, string CampaignDetail, string Notes) 
{ 
    var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files(0) : null; 
    if (file != null && file.ContentLength > 0) { 
     //If file is posted you will get here.. 
    } 
...... 
} 

提示:什麼是賞金參數? 邊界參數在末尾設置爲連字符加上隨機字符串,但您可以將其設置爲任何值。問題是,如果邊界字符串出現在請求數據中,它將被視爲邊界。

+0

正是我所做的修復它。不知道爲什麼,因爲它沒有得到很好的解釋。但它確實修復了它。 –

+0

請在我解釋的代碼中找到註釋。 –