无法使用PostFile上传图片

时间:2012-09-26 13:25:39

标签: servicestack

我正在尝试使用PostFile上传图片。作为一个简单的例子,我有以下DTO:

[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 个答案:

答案 0 :(得分:0)

PostFile执行HTTP POST 但是,由于您的示例代码第一行中的路线,您的服务仅允许GETPUTDELETE

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

<强>解决方案:

在您的路线中包含POST

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

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

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