English 中文(简体)
ADO.NET Data Services - Uploading files
原标题:

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.

问题回答

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:	emp" + 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:	emp	est.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.

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.





相关问题
ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

Synchronising SQL database through ADO.Net

The problem that i m having is how can i synchronise my datasets in my VS 2008 project to any changes in the database. As you know we read data from the db into the dataset which is disconnected, now ...

ADO.NET Data Services - Uploading files

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 ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

热门标签