如何使用 WCF 4.0 模板 (REST) 上传文件 [英] How to uploadfile using WCF 4.0 Template (REST)

查看:45
本文介绍了如何使用 WCF 4.0 模板 (REST) 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Restful webservice 中上传文件(500 MB 及以上...),然后将文件保存在特定位置?

How to upload file (500 MB size and up...) in Restful webservice then save the file in specific location?

如果你有链接,请分享.
我使用 Fiddler 来测试服务.

If you have links, please share.
I use Fiddler to test the service.

这是我一直在努力的代码.

Here are codes that I've been working for.

[WebInvoke(UriTemplate = "Add", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public bool UploadFile(FileUploader.File userFile, System.IO.Stream fileStream)
{
    FileUploader.Logic.StreamObject streamUploader = new FileUploader.Logic.StreamObject();
    streamUploader.UploadFile(userFile.FileName, fileStream);
    return true;
}



public class StreamObject : IStreamObject
{
    public void UploadFile(string filename, Stream fileStream)
    {
        byte[] buffer = new byte[10000];
        int bytesRead, totalbytesRead = 0;
        do
        {
            bytesRead = fileStream.Read(buffer, 0, buffer.Length);
            totalbytesRead += bytesRead;
        } while (bytesRead > 0);
    }
}

推荐答案

答案源于我之前所做的.该服务可能如下所示:

The answer stems from what I have done before. The service may look as follows:

[ServiceContract]
public class DocumentService
{
    [OperationContract]
    [WebTemplate("{name}"]
    public Document Create(Stream stream, string name)
    {
        var id = Guid.NewGuid().ToString("N");
        using(FileStream outputStream = File.Create(Path.Combine("c:\\temp\\", id)))
        {
            stream.CopyTo(outputStream);
        }
        Document document = new Document(); 
        document.Name = name;
        document.Id = id;
        // Save document to database
        // Set headers 
        return document;
    }
}

其中 Document 可能如下所示:

where Document may look as follows:

[DataContract]
public class Document
{
    [DataMember]
    public string Id
    {
       get;set;
    }

    [DataMember]
    public string Name
    {
       get;set;
    }

    /* other fields */

}

将文件上传到服务(假设它位于 http://api.example.com/documents/),您可以执行以下操作:

To upload a file to the service (assuming it is at http://api.example.com/documents/), you can do the following:

string name = "mydocument.doc";
var request = WebRequest.Create ("http://api.example.com/documents/" + name);
request.ContentType = "application/octet-stream";
request.Method = "POST";
using(var stream = request.GetRequestStream())
{
    using(var inputStream = File.OpenRead("c:\\mydocument.doc"))
    {
        inputStream.CopyTo(stream);
    }
}

using(var response = (HttpWebResponse)request.GetResponse())
{
    // process the response, if needed
}

如果您所做的只是发送一个流,则无需发送多部分流.任何参数(例如名称)都应该是 UriTemplate 的一部分,这一点很重要.

There is no need to send a multipart stream over if all you do is sending one stream. It is important that any parameters (such as name) should be part of the UriTemplate.

这篇关于如何使用 WCF 4.0 模板 (REST) 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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