2017-12-27 24 views
0

나는 여기에 붙어있어, 나는 indexedDb에서 json 개체를 동시에 서버에 IFormFile 개체로 게시하려고합니다. 그것을 받아들이는 방법은 다음과 같습니다 : 내가 JSON 개체로 내 batchModels를 저장했다, 단지 POST와 형태에서 바로 게시되기 전에json 개체 및 파일을 가져 오기 함께 asp.net 서버에 게시 할 수 있습니다

[HttpPost] 
    public async Task<IActionResult> Create(BatchModel model, IFormFile vinFile) 
    { 
    //logic goes here 
    } 

이 방법은 이전했다. 많은 그 이후 변경, 그는 다음 (간체) 방법, 오프라인 상태에서 다시 온라인수록 이제 클라이언트는 바로 업로드됩니다

테스트를 위해
if (infoChanged === true) { 
    fetchPromises.push(
     fetch('/Batch/Create/', { 
      headers: new Headers({ 
       'Content-Type': 'application/javascript' //Tried this with multi-part/form 
      }), 
      credentials: 'same-origin', 
      method: 'POST', 
      body: batch //, vinFile 
     }) 
    ); 
} 
return Promise.all(fetchPromises); 

내가 가진 방법을 사용하려 모델 만 채워졌고, 내가 바꾸어야했던 유일한 것은 C# 코드에서 [FromBody] 태그를 추가해야한다는 것이 었습니다.하지만 이제는 vinFile도 채워 져야합니다. FormData 객체와 함께 배치와 vinFile을 Create 함수와 같은 이름으로 추가하여 이미 시도했습니다. 하지만 두 변수 모두 null이됩니다.

답변

0

주요 기능은 헤더입니다. {Content-Type : "application/json"}

사용자가 원하는 경우 JSON에 대한 응답을 변경할 수 있습니다. 사용자가 직접 유형을 구성해야합니다.

fetch(myRequest).then(function(response) { 
    var contentType = response.headers.get("content-type"); 
    if(contentType && contentType.includes("application/json")) { 
     return response.json(); 
    } 
    throw new TypeError("Oops, we haven't got JSON!"); 
    }) 
    .then(function(json) { /* process your JSON further */ }) 
    .catch(function(error) { console.log(error); }); 

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

+0

당신은 또한 서버 측에서 당신의 유형을 변환하려는 경우 당신이에 대한 NewtonSoft https://www.newtonsoft.com/json –

+0

안녕하세요 덕분에 다이빙을 할 수 있습니다 응답하지만, 나는 '게시'를하지 않으려 고 노력하고있어. json과 .csv 파일을 동시에 게시하려고하기 때문에 응용 프로그램/json으로 변경할 수 없습니다. multipart/form-data에 대해 더 많이 생각하고 있었지만, 그 중 하나는 작동하지 않는 것 같습니다. 그래도 newtonsoft를 살펴볼 것입니다! – Typhaon

+0

문제가없는 행운을 빕니다. –