1
\$\begingroup\$

I am using building REST services for file upload on the server. Here is some code of that

[OprationContract]
[WebInvoke(Method = "POST", UriTemplate = "UploadFile/{fileName}")]
public string UploadFile(string fileName, Stream fileStream)
{
  string filePath = serverDirectory + "\\" + fileName;
  using (var output = File.Open(filePath, FileMode.Create))
  {
     fileStream.CopyTo(output);
  }
  return "success";
}

However this method works fine when file size is small. but when the file size is large say 30-40 Mb then the performance become low.

So I want to know what are the best practices to upload the files on the server with speed.

\$\endgroup\$
2
  • \$\begingroup\$ This code won't compile, your method isn't returning anything. \$\endgroup\$
    – svick
    Feb 8, 2014 at 15:01
  • \$\begingroup\$ @svick ohh I forgot to add that. Actaully I am returning my customer ServiceResponse object but for simplicity I didn't included that. \$\endgroup\$
    – Sachin
    Feb 8, 2014 at 15:07

1 Answer 1

2
\$\begingroup\$

The lines of code inside your UploadFile method look optimal to me.

Perhaps the problem is that your Stream contents are being fully buffered before they're being delivered.

I've no experience with this myself, but How to: Enable Streaming suggests you may need to change your configured TransferMode, and change your API so that it has a method whose only parameter is Stream (perhaps pass the filename in one method and the Stream in the next method).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.