PhoneGap - MVC - 文件上传 - Request.Content.ReadAsMultipartAsync抛出System.ArgumentException:路径中的非法字符

时间:2015-03-18 16:38:22

标签: c# cordova iis file-upload model-view-controller

我在Android和Android上运行了一个PhoneGap应用允许用户拍照并将其上传到MVC服务的iOS。这一切都很好,除非它抛出以下异常。

System.IO.IOException: Error writing MIME multipart body part to output
stream. ---> System.InvalidOperationException: The stream provider of type
'MyStreamProvider' threw an exception. ---> System.ArgumentException:
Illegal characters in path.

我认为罪魁祸首是三星Galaxy 4的用户 - 但我无法确认100%。看起来几乎就像设备提交的文件名与Windows文件名规则不兼容,但我不确定如何解决这个问题。

以下是完整代码:

public async Task<HttpResponseMessage> EnterSweep()
{
  // Check if the request contains multipart/form-data.
  if (!Request.Content.IsMimeMultipartContent())
  {
      throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
  }

  string root = HttpContext.Current.Server.MapPath("~/App_Data");
  string photoDirectory = HttpContext.Current.Server.MapPath("~/Photos");

  var provider = new MyStreamProvider(root);

  try
  {
    //This is the code that bombs
    await Request.Content.ReadAsMultipartAsync(provider);
  }
  catch (Exception e)
  {
    MyLogger.Error("Error uploading file", e);
    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
  }


  foreach (MultipartFileData file in provider.FileData)
  {
    RenameAndLogFile(file.LocalFileName, provider, photoDirectory);
  }

  return Request.CreateResponse(HttpStatusCode.OK);
}

这是完整的堆栈跟踪....

System.IO.IOException: Error writing MIME multipart body part to output
stream. ---> System.InvalidOperationException: The stream provider of type
'MyStreamProvider' threw an exception. ---> System.ArgumentException:
Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.Net.Http.MultipartFileStreamProvider.GetStream(HttpContent parent, HttpContentHeaders headers)
at System.Net.Http.MimeBodyPart.GetOutputStream()
--- End of inner exception stack trace ---
at System.Net.Http.MimeBodyPart.GetOutputStream()
at System.Net.Http.MimeBodyPart.<WriteSegment>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()
--- End of inner exception stack trace ---
at System.Net.Http.HttpContentMultipartExtensions.<MultipartReadAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpContentMultipartExtensions.<ReadAsMultipartAsync>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MyApp.Api.Controllers.SweepController.<EnterSweep>d__0.MoveNext()    

非常感谢,

1 个答案:

答案 0 :(得分:0)

我看到一些Galaxy手机上发生了奇怪的事情(仅限),也有HTML渲染。这并不让我感到惊讶,但我认为这里需要更多的信息来提供真正的解决方案。我可以建议一些事情来帮助你吗?

  1. 将文件名写入日志记录,然后您可以查看您的怀疑是否正确。可能是Galaxy没有很好地处理特定角色,这应该是显而易见的。

  2. 为用户输入添加遮罩和验证。首先做第一步可能会让你更好地了解其工作原理,但一般来说,确保你有掩码输入以防止麻烦或恶意输入是一个好主意。它也应该尽可能在服务器上进行验证。那里有很多JS面具,对你有用的很大程度上取决于你的用例。

  3. 如果您确定了导致问​​题的原因或获取更多详细信息,请回复。