我正在尝试使用 restClient (RestSharp)请求从c#类上传文件。 我正在创建Method.POST方法并向此请求添加一个音频文件作为multipart / form-data。
执行请求时,服务器会抛出异常。
例外:
System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at Groove.Libraries.Helper.EnumHelper.ParseEnum[T](String value) in D:\project\Groove\Web\Groove\Libraries\Helper\EnumHelper.cs:line 47
at Groove.Controllers.Api.DocumentController.
<PostDocumentUpload>d__5.MoveNext()
RestClient请求代码:
string api_url = "http://localhost:57997/";
var fullFileName = "Adios.mp3";
var filepath = @"C:\Users\Admin\Desktop\Adios.mp3";
RestClient client = new RestClient(ApiModel.api_url);
var request = new RestRequest("api/document", Method.POST);
request.AddFile(Path.GetFileNameWithoutExtension(fullFileName), filepath);
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("ReferenceType",28,ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
服务器代码:
public async Task<object> PostDocumentUpload()
{
try
{
// Make temp physical path where file to be saved
var tempPath =
HttpContext.Current.Server.MapPath(string.Format("{0}/{1}", Constants.MediaResourceFolder,
Constants.MediaResorceTempFolder));
// Check temporary path is exist or not, if not then create temporary folder
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
// Create object of MyMultipartFormDataStreamProvider class
var stream = new MultipartHelper(tempPath);
// Save file at temp path
await RequestContext.Request.Content.ReadAsMultipartAsync(stream);
// Exception throw from Here,
ReferenceType referenceType = EnumHelper.ParseEnum<ReferenceType>(stream.FormData["ReferenceType"]);
// -------------other code
}
catch (Exception ex)
{
return FailureResponse(ex);
}
我猜问题是stream.FormData [&#34; ReferenceType&#34;]未按要求正确设置。
我不想更改服务器代码。因为当你从邮递员或浏览器打电话时它工作正常。
有人可以帮忙吗?感谢
答案 0 :(得分:0)
我通过更改几个代码找到了解决方案。
更新代码:
inkCanvas.InkPresenter.InputProcessingConfiguration.RightDragAction = InkInputRightDragAction.LeaveUnprocessed;