0

FormData Post我的问题是,当我在此处检查多部分数据时,我的 c# api 控制器正在回退请求: 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

  • 请求有效载荷

------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我缺少什么选项来工作。

谢谢你。

4

1 回答 1

0

我删除了多部分/边界标题属性,让浏览器为您处理它。上传工作。

于 2018-06-11T21:10:35.983 回答