ADO.NET 数据服务 - 上传文件
我正在尝试编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已使用 POST 将文件上传到 ADO.NET 数据服务,但我不确定这是否是推荐的方法。我的方法是:
在数据服务上,我实现了一个名为 UploadFile 的服务操作(使用 WebInvoke 属性,以便满足 POST 调用):
然后在客户端,我使用以下方式调用数据服务:
这使用 WebClient上传文件,将文件数据放入 HttpRequest.Files 集合中并设置内容类型。如果您希望自己发送文件的内容(例如,从 Asp FileUpload 控件)而不是让 webClient 使用文件路径读取文件,则可以使用类似于 这篇文章。 来代替使用
尽管您可以使用传入的字节数组
。我希望这会有所帮助。
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):
Then on the client side I call the data service using:
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
you could use a byte array that you pass in.
I hope this helps.
我不是 100% 确定如何直接对文件服务器本身执行此操作,但 ADO.Net 数据服务肯定支持类似于数据库的功能。下面的代码是如何实现将文件放入数据库的类似目标的。不确定这会有多大帮助,但
请注意:此代码还使用实体框架创建 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
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.