ADO.NET数据服务 - 上传文件 [英] ADO.NET Data Services - Uploading files

查看:255
本文介绍了ADO.NET数据服务 - 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写我们的客户可以通过它上传文件我们的文件服务器上的REST Web服务。有一个例子或任何有用的链接,我可以指任何指导?

I am trying to write REST web service through which our clients can upload a file on our file server. IS there an example or any useful links which I can refer for any guidance?

我还没有看到POST操作的例子很多利用现有的ADO.NET数据服务。

I haven't seen many examples of POST operation using ADO.NET data services available.

推荐答案

我已经上传文件到ADO.NET DataService的使用POST,虽然我不知道它是否是推荐的方法。我开始有计划的方式是:

I've uploaded a file to ADO.NET dataservices using POST although I'm not sure whether it's the recommended approach. The way I went about it is:

在DataService在我实现了所谓的UploadFile(使用WebInvoke属性,以便它迎合了POST调用)服务操作:

On the dataservice I've implemented a service operation called UploadFile (using the WebInvoke attribute so that it caters for POST calls):

[WebInvoke]
public void UploadFile()
{
   var request = HttpContext.Current.Request;

   for (int i = 0; i < request.Files.Count; i++)
   {
       var file = request.Files[i];
       var inputValues = new byte[file.ContentLength];

       using (var requestStream = file.InputStream)
       {
           requestStream.Read(inputValues, 0, file.ContentLength);
       }

       File.WriteAllBytes(@"c:\temp\" + file.FileName, inputValues);
   }
}

然后在客户端的我称之为使用数据服务:

Then on the client side I call the data service using:

var urlString = "http://localhost/TestDataServicePost/CustomDataService.svc/UploadFile";
var webClient = new WebClient();
webClient.UploadFile(urlString, "POST", @"C:\temp\test.txt");

此使用Web客户端上传哪些地方在Htt的prequest.Files收集文件数据和设置内容类型的文件。如果你想preFER发送自己的文件(例如从ASP FileUpload控件)的内容,而不是Web客户端读取使用的路径,文件的文件,你可以使用类似的方式,它是在做了的WebRequest 的职位。虽然不是用

This uses a WebClient to upload the file which places the file data in the HttpRequest.Files collection and sets the content type. If you would prefer to send the contents of the file yourself (eg from an Asp FileUpload control) rather than the webClient reading a file using a path to the file, you can use a WebRequest similar to the way that it's done in this post. Although instead of using

FileStream fileStream = new FileStream(uploadfile, 
                                FileMode.Open, FileAccess.Read);

您可以使用您传递一个字节数组中。

you could use a byte array that you pass in.

我希望这有助于。

这篇关于ADO.NET数据服务 - 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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