2016-11-18 7 views
0

ファイルといくつかのデータをMVC 5バックエンドに投稿しようとしています。MVC5 404エラーファイルと入力ポストAngularjs

問題が正しくマッピングされていないため、404が返されます.Http投稿要求がmultipart/form-dataコンテンツタイプとして送信されています。ここで

[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

(mvcmapping atrributesを使用して)私はこの要求を受信しようとしているコントローラMVC5バックエンドの角度サービスここ

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してくださいされています以下を試してください

MVCコントローラで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() { 

      }); 
     } 

[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

私が説明したコードでコメントを見つけてください。 –

関連する問題