我正在尝试将包含任意二进制数据的文件作为multipart / form-data发布到WebAPI 2控制器。
尝试使用以下内容读取文件时:
var provider = new MultipartFormDataStreamProvider(uploadDirectoryPath);
await Request.Content.ReadAsMultipartAsync(provider);
我明白了:
System.InvalidOperationException: The character set provided in ContentType is invalid.
Cannot read content as string using an invalid character set.
---> System.ArgumentException: '"utf-8"' is not a supported encoding name.
For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name
我理解问题是utf-8周围的引号,内部使用的HttpContent.ReadAsStringAsync()
中的this错误是here所解释的。
请求是从Unity3D应用程序发送的,我无法更改请求正文中的字符集。
以下是我在Unity3D脚本中用于创建POST请求的代码。
WWW chunkFile = new WWW("file:///" + Path.Combine(tempZipFolderPath, chunkFilenames[j]));
yield return chunkFile;
if (chunkFile.error != null)
throw new Exception(string.Format("{0} error opening file {1}", chunkFile.error, chunkFilenames[j]));
//BUGFIX: Not using IMultipartFormSection because of https://fogbugz.unity3d.com/default.asp?826626_htlchp13nh8th2to
var form = new WWWForm();
form.AddField("chunkNumber", chunkNumber);
form.AddField("totalChunks", totalChunks);
form.AddField("identifier", tempZipFileName);
form.AddBinaryData("filename", chunkFile.bytes, chunkFilenames[j]);
var scanUploadPostRequest = UnityWebRequest.Post(WebApiScanUploadUrl, form);
yield return scanUploadPostRequest.Send();
//BUGBUG: charset UTF-8 wrapped in quotes causes error
有解决方法吗?