2012-09-26 52 views
1

我想使用PostFile上传图像。举一个简单的例子,我有以下DTO:无法使用PostFile上传图像

[Route("/Pictures/{Id}", "GET, PUT, DELETE")] 
public class Picture 
{ 
    public int Id { get; set; } 
} 

public class PictureResponse : IHasResponseStatus 
{ 
    public int Id { get; set; } 

    #region Implementation of IHasResponseStatus 

    public ResponseStatus ResponseStatus { get; set; } 

    #endregion 
} 

我GET正常工作:

public override object OnGet(Picture request) 
{ 
    var memoryStream = new MemoryStream(); 
    PictureRepository.Get(request.Id).Save(memoryStream, ImageFormat.Png); 
    return new HttpResult(memoryStream, "image/png"); 
} 

但我PostFile炸毁:

var imagePathInfo = new FileInfo(@"C:\Users\Mark\Downloads\avatars\symang.jpg"); 
var serviceClient = new JsonServiceClient("http://localhost:52712/api") 
serviceClient.PostFile<PictureResponse>("/Pictures/{0}".Fmt(id), imagePathInfo, MimeTypes.GetMimeType(imagePathInfo.Name)); 

这里的错误和堆栈跟踪:

Server Error in '/' Application. 

An existing connection was forcibly closed by the remote host 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 

Source Error: 


Line 32:    var imagePathInfo = new FileInfo(@"C:\Users\Mark\Downloads\avatars\symang.jpg"); 
Line 33:    var serviceClient = new JsonServiceClient("http://localhost:52712/api"); 
Line 34:    serviceClient.PostFile<PictureResponse>("/Pictures/{0}".Fmt(id), imagePathInfo, MimeTypes.GetMimeType(imagePathInfo.Name)); 
Line 35:    RedirectToAction("Index"); 
Line 36:    return View(); 

Source File: C:\Users\Mark\Documents\Visual Studio 2010\Projects\Sandbox\UploadFileAttachments\UploadFileAttachments\Controllers\HomeController.cs Line: 34 

Stack Trace: 


[SocketException (0x2746): An existing connection was forcibly closed by the remote host] 
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +6210712 
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +134 

[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.] 
System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) +318 
System.Net.HttpWebRequest.MakeMemoryStream(Stream stream) +221 

[WebException: The underlying connection was closed: An unexpected error occurred on a receive.] 
System.Net.HttpWebRequest.GetResponse() +6115603 
ServiceStack.ServiceClient.Web.ServiceClientBase.PostFile(String relativeOrAbsoluteUrl, Stream fileToUpload, String fileName, String mimeType) in C:\src\ServiceStack\src\ServiceStack.Common\ServiceClient.Web\ServiceClientBase.cs:815 
ServiceStack.ServiceClient.Web.ServiceClientBase.PostFile(String relativeOrAbsoluteUrl, FileInfo fileToUpload, String mimeType) in C:\src\ServiceStack\src\ServiceStack.Common\ServiceClient.Web\ServiceClientBase.cs:790 
UploadFileAttachments.Controllers.HomeController.ChangePicture(Int32 id) in C:\Users\Mark\Documents\Visual Studio 2010\Projects\Sandbox\UploadFileAttachments\UploadFileAttachments\Controllers\HomeController.cs:34 
lambda_method(Closure , ControllerBase , Object[]) +150 
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17 
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27 
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263 
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191 
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343 
System.Web.Mvc.Controller.ExecuteCore() +116 
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97 
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10 
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37 
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12 
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50 
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7 
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22 
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970061 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

我确定我必须误解如何使用PostFile。顺便说一句,我从未在我的服务上打过我的OnPost方法。感谢您的任何见解。

回答

1

PostFile做了一个HTTP POST
但你的服务只允许GETPUTDELETE因为在您的示例代码的第一行的路线:

[Route("/Pictures/{Id}", "GET, PUT, DELETE")] 

解决方案:

无论是包括POST在您的路线:

[Route("/Pictures/{Id}", "GET, PUT, DELETE, POST")] 

...或者只是省略HTTP动词:

[Route("/Pictures/{Id}")]