I'm currently having an issue while uploading a file from Html to my rest service (WCF REST). While uploading a file, I'd like to send information like Title and Description along with the file's Contents.
So, I've created a test form like this:
<form id="testForm" action="http://localhost.:1576/NotepadService.svc/Note/91f6413c-4d72-42ca-a0f3-38df15759fc9/Attachment" method="POST" enctype="multipart/form-data">
<table>
<tr><td>Title:</td><td><input type="text" name="Title"></td></tr>
<tr><td>Description:</td><td><input type="text" name="Description"></td></tr>
<tr><td>Filename:</td><td><input type="text" name="Filename"></td></tr>
<tr><td>File:</td><td><input type="file" name="Contents"></td></tr>
<tr><td/><td><input type="submit" value="Send"></td></tr>
</table>
</form>
Server-side, I'd like to translate it to this method:
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare,
Method = "POST",
UriTemplate = "/Note/{noteId}/Attachment")]
[Description("Add an attachment to a Note.")]
void AddAttachmentToNote(string noteId, AttachmentRequestDto attachmentRequestDto);
With AttachmentRequestDto defined as
[DataContract]
public class AttachmentRequestDto
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public string Filename { get; set; }
[DataMember]
public Stream Contents { get; set; }
}
So, long story short, I'd like to get the Title and Description as string values, while getting the contents of the file as a stream. This doesn't seem to work, as the html form will put all the contents of the form (so also Title and Description) into a stream, along with the contents of the file. Therefor, defining my REST method as
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Bare,
Method = "POST",
UriTemplate = "/Note/{noteId}/Attachment")]
[Description("Add an attachment to a Note.")]
void AddAttachmentToNote(string noteId, Stream formContents);
works, but then I need to parse the stream to get all of my data (which is not a nice approach, compared to what I actually want to do).
Maybe I need to define 2 different service methods, one accepting only the file, and the other accepting the file's details? That would, however, mean that my business rules (Title required + filecontents required) should be validated differently (as REST is stateless).
Something that might be worth mentioning: I need to save the file's contents in the database, not on the file system.
Does anyone have some input on this one? I'm kind of stuck on it...
Thanks!
Please find some code that might help you to pass a file along with its details in a single call to the REST service:
First you would need something called a MultipartParser as shown below:
Now define your REST method as shown:
Now the implemenation of the above method as shown:
My AttachmentRequestDto looks as shown:
Now from the client i do perform a POST as shown :
I do use a third party dll for the above code called RESTSharp.
Once on the server the MultipartParser identifies your request boundary to split the necessary contents and then you can decide on what to do with each of the split contents (save to file,db, etc..)
Just make sure you have the approriate config entries for the above as well along with dataContractSerializer property and readerQuotas set for the webHttpBinding.
NOTE: The MultipartParser can be more re-factored if needed.