I'm not sure how you want to upload a file using REST. But a file can certainly be your resource that your REST service exposes. The AtomPubTaskService sample really shows how to do this in an Atom Publishing Protocol way.
Or, do you mean you want to GET, POST, PUT, DELETE files at a RESTful service? In that case, you would probably want to start by defining your service resources. If this was some sort of file service, I would just define a resource for a file location.
Something like D:\RestService\Users. Then I would make uritemplates for <baseuri>/{directory} and <baseuri>/{directory}/{file} or something like that for all the defined resources. Next thing to do is consider what the above verbs do to the resources listed
out. Is this the kind of thing you are looking at doing?
At a high level, you basically need to have the client side read the file to a stream and send the stream to the server. Next, the server gets the message, parses the uri and/or method (post) to determine which operation to invoke. That operation should
take the stream and write it to a file locally.
Now, the details can get a bit tricky, especially if you are writing a client only, and not the server component. You need to know the content-type expected by the service for a Post, as well as the format of the content. At this point, to issue POST,
you need to use HttpWebRequest to manually build the POST message.
Can you provide more details on what you are trying to build? A service that accepts files posted by others? Or a client/web application that uploads to an existing service? What is the transport/protocol?
Stream encodedAudio will contain the body of the POST. The key is to POST your data with a content-type of raw. base64EncodedData is obviously the base64 encoded data.
HttpWebRequest request = WebRequest.Create( address ) as HttpWebRequest;
// Set type to POST request.Method = "POST"; request.ContentType = "text/raw;";
StringBuilder data = new StringBuilder(); data.Append( base64EncodedData );
// Create a byte array of the data we want to send byte[] byteData = Encoding.UTF8.GetBytes( data.ToString() );
// Set the content length in the request headers request.ContentLength = byteData.Length;
// Write data using ( Stream postStream = request.GetRequestStream() ) { postStream.Write( byteData, 0, byteData.Length ); }
// Get response using ( HttpWebResponse response = request.GetResponse() as HttpWebResponse ) { // Get the response stream StreamReader reader = new StreamReader( response.GetResponseStream() );
None
0 Points
11 Posts
Upload file with REST
Jan 28, 2009 07:37 AM|OrchideSwe|LINK
Hello!
Is it possible to upload a file via a rest service?
Can anoyone suggest any good articles or examples related to this.
Best regards,
Jonas Karlsson
Member
10 Points
30 Posts
Re: Upload file with REST
Jan 29, 2009 06:58 PM|James Osborne|LINK
I'm not sure how you want to upload a file using REST. But a file can certainly be your resource that your REST service exposes. The AtomPubTaskService sample really shows how to do this in an Atom Publishing Protocol way.
Or, do you mean you want to GET, POST, PUT, DELETE files at a RESTful service? In that case, you would probably want to start by defining your service resources. If this was some sort of file service, I would just define a resource for a file location. Something like D:\RestService\Users. Then I would make uritemplates for <baseuri>/{directory} and <baseuri>/{directory}/{file} or something like that for all the defined resources. Next thing to do is consider what the above verbs do to the resources listed out. Is this the kind of thing you are looking at doing?
-James
None
0 Points
11 Posts
Re: Upload file with REST
Feb 03, 2009 05:21 AM|OrchideSwe|LINK
Yepp, I only want to POST files to a restful service. url/{foldertosaveto}/{filename}.
But I haven´t got a clue how to solve it... :)
Got any examples?
Regards,
Jonas
Member
10 Points
30 Posts
Re: Upload file with REST
Feb 04, 2009 12:57 PM|James Osborne|LINK
At a high level, you basically need to have the client side read the file to a stream and send the stream to the server. Next, the server gets the message, parses the uri and/or method (post) to determine which operation to invoke. That operation should take the stream and write it to a file locally.
Now, the details can get a bit tricky, especially if you are writing a client only, and not the server component. You need to know the content-type expected by the service for a Post, as well as the format of the content. At this point, to issue POST, you need to use HttpWebRequest to manually build the POST message.
None
0 Points
11 Posts
Re: Upload file with REST
Feb 04, 2009 01:03 PM|aramana|LINK
Hi, Jonas
Can you provide more details on what you are trying to build? A service that accepts files posted by others? Or a client/web application that uploads to an existing service? What is the transport/protocol?
Thanks
Anand Ramanathan
Program Manager
WCF REST team
Microsoft
Member
730 Points
409 Posts
Re: Upload file with REST
Apr 20, 2009 03:01 PM|SuperGhost|LINK
I think I have accomplished what you're asking about.
The basic idea is I want to send base64 data to my web service via HTTP POST. The base64 encoded data consists of the file I wanted to upload.
Here's my method:
Stream encodedAudio will contain the body of the POST. The key is to POST your data with a content-type of raw. base64EncodedData is obviously the base64 encoded data.
Member
30 Points
8 Posts
Re: Upload file with REST
Apr 14, 2010 03:13 AM|PamidiVenkat|LINK
Hi ,
Check below post,
http://stackoverflow.com/questions/664712/restful-wcf-service-image-upload-problem
Hope This will Help.
Regards,
Venkat