ServiceStack - 使用流和 Uri 上传文件 [英] ServiceStack - Upload files with stream and with Uri

查看:37
本文介绍了ServiceStack - 使用流和 Uri 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 DTO:

[Route("/images/{imageId}/upload", "PUT")]公共类 GetImageWithStream : IRequiresRequestStream{公共流请求流 { 获取;放;}公共字符串 imageId { 获取;放;}}///images/{imageId}/upload?url={imageUrl}[Route("/images/{imageId}/upload", "PUT")]公共类 GetImageWithUri{公共字符串 imageId { 获取;放;}公共字符串 url { 获取;放;}}

/images/1/upload -> 这应该路由到第一个 DTO

/images/1/upload?url=something.jpg -> 这应该路由到第二个 DTO

现在它们都路由到第一个 DTO,在第二个路径的情况下,流当然是 N​​ullStream.使用第一个路径,流很好,但 imageId 为空.

或者我可以想象这样的事情:

[Route("/images/{imageId}/upload", "PUT")]公共类 GetImageWithStream : IRequiresRequestStream{公共流请求流 { 获取;放;}公共字符串 imageId { 获取;放;}公共字符串 url { 获取;放;}}

是否可以在 ServiceStack 中以不同的方式处理相同的 PATH?

解决方案

使用 IRequiresRequestStream 告诉 ServiceStack 跳过请求绑定并让您的服务从未缓冲的请求流中读取.

但如果您只想处理文件上传可以使用 RequestContext.Files 独立于请求 DTO 访问上传的文件.例如:

public object Post(MyFileUpload request){如果(this.Request.Files.Length > 0){var uploadFile = this.Request.Files[0];UploadFile.SaveTo(MyUploadsDirPath.CombineWith(file.FileName));}返回 HttpResult.Redirect("/");}

ServiceStack 的 imgur.servicestack.net 示例展示了如何访问 多个上传文件的字节流,例如:

public object Post(上传请求){foreach (在 Request.Files 中 var uploadFile.Where(uploadedFile =>uploadedFile.ContentLength > 0)){使用 (var ms = new MemoryStream()){UploadFile.WriteTo(ms);写入图像(毫秒);}}返回 HttpResult.Redirect("/");}

I have got the following DTOs:

[Route("/images/{imageId}/upload", "PUT")]
public class GetImageWithStream : IRequiresRequestStream
{
    public Stream RequestStream { get; set; }
    public string imageId { get; set; }
}

///images/{imageId}/upload?url={imageUrl}
[Route("/images/{imageId}/upload", "PUT")]
public class GetImageWithUri 
{
    public string imageId { get; set; }
    public string url { get; set; }
}

/images/1/upload -> this should routed to the first DTO

/images/1/upload?url=something.jpg -> this should routed to the second DTO

Now both of them routed to the first DTO and in case of the second path the stream is a NullStream of course. With the first path the stream is good but the imageId is null.

Or I can imagine something like this:

[Route("/images/{imageId}/upload", "PUT")]
public class GetImageWithStream : IRequiresRequestStream
{
    public Stream RequestStream { get; set; }
    public string imageId { get; set; }
    public string url { get; set; }
}

Is it possible to handle the same PATH with different ways in ServiceStack?

解决方案

Using a IRequiresRequestStream tells ServiceStack to skip the Request Binding and to let your Service read from the un-buffered request stream.

But if you just want to handle file uploads you can access uploaded files independently of the Request DTO using RequestContext.Files. e.g:

public object Post(MyFileUpload request)
{
    if (this.Request.Files.Length > 0)
    {
        var uploadedFile = this.Request.Files[0];
        uploadedFile.SaveTo(MyUploadsDirPath.CombineWith(file.FileName));
    }
    return HttpResult.Redirect("/");
}

ServiceStack's imgur.servicestack.net example shows how to access the byte stream of multiple uploaded files, e.g:

public object Post(Upload request)
{
    foreach (var uploadedFile in Request.Files
       .Where(uploadedFile => uploadedFile.ContentLength > 0))
    {
        using (var ms = new MemoryStream())
        {
            uploadedFile.WriteTo(ms);
            WriteImage(ms);
        }
    }
    return HttpResult.Redirect("/");
}

这篇关于ServiceStack - 使用流和 Uri 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆