ADO.NET 数据服务 - 上传文件

发布于 2024-08-10 19:23:23 字数 125 浏览 8 评论 0原文

我正在尝试编写 REST Web 服务,通过该服务我们的客户可以将文件上传到我们的文件服务器上。是否有示例或任何有用的链接可供我参考以获取指导?

我还没有看到很多使用 ADO.NET 数据服务进行 POST 操作的示例。

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-08-17 19:23:23

我已使用 POST 将文件上传到 ADO.NET 数据服务,但我不确定这是否是推荐的方法。我的方法是:

在数据服务上,我实现了一个名为 UploadFile 的服务操作(使用 WebInvoke 属性,以便满足 POST 调用):

[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);
   }
}

然后在客户端,我使用以下方式调用数据服务:

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

这使用 WebClient上传文件,将文件数据放入 HttpRequest.Files 集合中并设置内容类型。如果您希望自己发送文件的内容(例如,从 Asp FileUpload 控件)而不是让 webClient 使用文件路径读取文件,则可以使用类似于 这篇文章。 来代替使用

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

尽管您可以使用传入的字节数组

。我希望这会有所帮助。

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:

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");

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.

I hope this helps.

甜`诱少女 2024-08-17 19:23:23

我不是 100% 确定如何直接对文件服务器本身执行此操作,但 ADO.Net 数据服务肯定支持类似于数据库的功能。下面的代码是如何实现将文件放入数据库的类似目标的。不确定这会有多大帮助,但

var myDocumentRepositoryUri = new Uri("uri here");
var dataContext = new FileRepositoryEntities(myDocumentRepositoryUri);
var myFile = new FileItem();
myfile.Filename = "upload.dat";
myFile.Data = new byte[1000]; // or put whatever file data you want to here
dataContext.AddToFileItem(myFile);
dataContext.SaveChanges();

请注意:此代码还使用实体框架创建 FileItem(将数据库表表示为对象)并保存该数据。

I'm not 100% sure how to do this directly to a file server per se, but ADO.Net Data Services definitely support something similar to a database. The code below is how a similar goal of putting a file into a database has been accomplished. Not sure how much that will help, but

var myDocumentRepositoryUri = new Uri("uri here");
var dataContext = new FileRepositoryEntities(myDocumentRepositoryUri);
var myFile = new FileItem();
myfile.Filename = "upload.dat";
myFile.Data = new byte[1000]; // or put whatever file data you want to here
dataContext.AddToFileItem(myFile);
dataContext.SaveChanges();

Note: this code is also using Entity Framework to create a FileItem (representation of a database table as an object) and to save that data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文