通过html5 FormData将multipart / form-data发布到c#Api

时间:2018-05-30 16:21:54

标签: asp.net-web-api multipartform-data form-data http-options-method

我的问题是,当我在这里检查多部分数据时,我的c#api控制器正在回复FormData Post请求:IsMimeMultipartContent(),然后将消息重新发送回UI:

415 (Unsupported Media Type)

 [HttpPost]
 [Route("MediaUpload")]
public async Task<HttpResponseMessage> MediaUpload([FromUri]string sessionId, [FromUri]string patientId)
    {

        if (!Request.Content.IsMimeMultipartContent())
        {    // *** ALWAYS THROWS ERROR ***
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }
        
        //access form data  
        var provider = await Request.Content.ReadAsMultipartAsync(new InMemoryMultipartFormDataStreamProvider());            
        NameValueCollection formData = provider.FormData;
            
        //... additional code omitted
        
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("UploadPath", fullPath);
        response.Headers.Add("Access-Control-Allow-Origin", "*");
        return response;
    }

我的前端有效负载如下所示:

  • 请求标题

POST /api/import/MediaUpload?sessionID=c83f9589-742e-40e3-8cf5-7ffff141c3d7&patientId=5981 HTTP/1.1
Host: localhost:56703
Connection: keep-alive
Content-Length: 6545
Accept: application/json, text/plain, */*
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
Content-Type: multipart/form-data
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

  • 请求PAYLOAD

------WebKitFormBoundaryVrKSHPqdT9c6JkKv
Content-Disposition: form-data; name="enctype"

multipart/form-data
------WebKitFormBoundaryVrKSHPqdT9c6JkKv
Content-Disposition: form-data; name="MediaInfo"

[{"PatientID":5981,"PatientLastName":"Hobo","PatientFirstName":"John","DeviceID":"123","InstanceID":89,"PatientDOB":"1/3/1970","FileName":"image2.jpg","FileSize":5880,"ExamDate":"5/30/2018","PatientID":66665981,"SessionID":"sessionID=9999141c3d7"}]
------WebKitFormBoundaryVrKSHPqdT9c6JkKv
Content-Disposition: form-data; name="files[]"; filename="image2.jpg"
Content-Type: image/jpeg

ÿØÿàJFIFÿÛC															

------WebKitFormBoundaryVrKSHPqdT9c6JkKv--

我的前端正在发送FormData,使用以下TypeScript方法生成,然后在下面进一步调用importService

save() {
	let params = { patientID: ''};
	let exDate = new Date(this.defaultDate).toLocaleDateString();
	
	// Populate MediaInfo object
	let minfo = this.SetMediaArray();
	params.pID = minfo[0].PID;
	
	const formData = new FormData();
	formData.set("enctype", "multipart/form-data" );	// doesn't make a difference..
	formData.append("MedInfo",  JSON.stringify(minfo));

	for(var i=0; i<this.importImages.length; i++){
		// append images to the files[] array, then send formData object to impService
		formData.append("files[]", this.dataURItoBlob(this.importImages[i].TnUrl), this.importImages[i].name);
	}

	let endpoint = this.selectedObj.Url;			
	
	this.importService.saveImportObjects(formData, params, endpoint).subscribe(
		data=> {			
			console.log(data);			
		},
		err => {         
			console.log(err);			
		}
	); 
}

所以基本上我想知道我错过了什么http选项才能使用。

谢谢。

1 个答案:

答案 0 :(得分:0)

我删除了multipart / boundary标头属性,让浏览器为您处理。上传工作。