[英]Post Content-Type undefined and application/json at the same time
I am trying to upload file and post some data at the same time. 我正在尝试上传文件并同时发布一些数据。 The value of topicSelected is java script object array.
topicSelected的值是java脚本对象数组。 When I check in the c# controller the value of topicSelected is null.
当我检入c#控制器时,topicSelected的值为null。 When I check the value in my service everything is OK.
当我检查我的服务中的值时,一切正常。 I am thinking that the problem is that I am not specifying the application/json type.
我认为问题是我没有指定application / json类型。
setNewVideoRecord = function(file, videoName, videoVersion, topicSelected) {
console.log(topicSelected);
var self = this;
var formData = new FormData();
formData.append('file', file);
formData.append('videoName', videoName);
formData.append('videoVersion', videoVersion);
formData.append('topicSelected', topicSelected);
$http.post(self.baseUrl + "Admin/uploadVideoFile", formData, {
withCredentials: true,
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity
}).then(function onSuccess(response) {
self.fileNameUpload = null;
})
}
You don't have an undefined content type. 您没有未定义的内容类型。 Telling your library that Content-Type is undefined just stops it trying to set its own default and let the underlying XMLHttpRequest object figure it out for itself.
告诉你的图书馆Content-Type是未定义的,只是停止尝试设置自己的默认值,让底层的XMLHttpRequest对象为自己找出它。 Since you are passing it a FormData object, it will examine that to identify the correct content-type (which is be
multipart/mixed
). 由于您传递的是FormData对象,因此它会检查它以识别正确的内容类型(
multipart/mixed
)。
This will encode each piece of data (including the file you are uploading) as a separate part, each of which will have its own content-type set automatically. 这会将每个数据(包括您上传的文件)编码为一个单独的部分,每个部分都会自动设置自己的内容类型。
You can't make XHR encode the non-file data as JSON. 您无法使XHR将非文件数据编码为JSON。 It will use the standard multipart format with a separate part for each of the non-file inputs (and another part for the file).
它将使用标准的多部分格式,每个非文件输入都有一个单独的部分(文件的另一部分)。
You need to write the server side code to expect that format and not JSON. 您需要编写服务器端代码以期望该格式而不是JSON。
Since topicSelected
is a complex data structure (and not a string) you can run it through JSON.stringify()
before appending it to the form data object. 由于
topicSelected
是一个复杂的数据结构(而不是字符串),因此您可以在将其附加到表单数据对象之前通过JSON.stringify()
运行它。
Once you read the topicSelected
string, you can parse it from JSON on the server. 阅读
topicSelected
字符串后,您可以从服务器上的JSON解析它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.