8

Is there a way to upload a file from local filesystem to a folder in a server using ASMX web services(no WCF, don't ask why:)?

UPD

P.S.file size can be 2-10 GB

2xMax
  • 1,639
  • 6
  • 21
  • 41

5 Answers5

10

Sure:

[WebMethod]
public void Upload(byte[] contents, string filename)
{
    var appData = Server.MapPath("~/App_Data");
    var file = Path.Combine(appData, Path.GetFileName(filename));
    File.WriteAllBytes(file, contents);
}

then expose the service, generate a client proxy from the WSDL, invoke, standard stuff.

--

UPDATE:

I see your update now about handling large files. The MTOM protocol with streaming which is built into WCF is optimized for handling such scenarios.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @scartag, yes that's what I meant. – Darin Dimitrov Jan 25 '11 at 22:21
  • @2xMax, for big files you're gonna have a problem :-) You will need to increase the maximum request size in web.config and for really big files you have a bigger problem. There are advanced protocols such as MTOM and streaming which are [built-into WCF](http://msdn.microsoft.com/en-us/library/ms733742.aspx) but with ASMX you are gonna have to implement. IIRC there were some SOAP extensions for ASMX that could be used with MTOM but at this stage they should be sooo much deprecated that I wouldn't even think about it. – Darin Dimitrov Jan 25 '11 at 22:23
  • . i thought so :) ... @2xMax you can transfer large files if you have the required connection speed/bandwidth ... lest it times out ... – scartag Jan 25 '11 at 22:24
3

When developing my free tool to upload large files to a server, I am also using .NET 2.0 and web services.

To make the application more error tolerant for very large files, I decided to not upload one large byte[] array but instead do a "chuncked" upload.

I.e. for uploading a 1 MB file, I do call my upload SOAP function 20 times, each call passing a byte[] array of 50 KB and concating it on the server together again.

I also count the packages, when one drops, I try to upload it again for several times.

This makes the upload more error tolerant and more responsive in the UI.

If you are interested, this is a CP article of the tool.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
0

For very large files, the only efficient way to send them to web services is with MTOM. And MTOM is only supported in WCF, which you have ruled out. The only way to do this with old-style .asmx web services is the answer that @Darin Dimitrov gave. And with that solution, you'll have to suffer the cost of the file being base64 encoded (33% more bandwidth).

Erv Walter
  • 13,737
  • 8
  • 44
  • 57
0

We had the same requirement, basically uploading a file via HTTP POST using the standard FileUpload controls on the client side. In the end we just added an ASPX page to the ASMX web service project (after all its just a web project) - this allowed us to upload to i.e. http://foo/bar/Upload.aspx when the web service was at http://foo/bar/baz.asmx. This kept the functionality within the web service, even though it was using a separate web page.

This might or might not fit your requirements, @Darins approach would work as a workaround as well but you would have to make modifications on the client side for that, which wasn't an option for us.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
0

You can try to convert the file to Base64 and pass it as a string to the service and then convert back to a byte array.

https://forums.asp.net/t/1980031.aspx?Web+Service+method+with+Byte+array+parameter+throws+ArgumentException

How to convert file to base64 in JavaScript?

The input is not a valid Base-64 string as it contains a non-base 64 character

IHAFURR
  • 111
  • 4