开发者

ASMX file upload

开发者 https://www.devze.com 2023-02-07 04:52 出处:网络
Is there a way to upload a file from local filesystem to a folder in a开发者_如何学Go server using ASMX web services(no WCF, don\'t ask why:)?

Is there a way to upload a file from local filesystem to a folder in a开发者_如何学Go server using ASMX web services(no WCF, don't ask why:)?

UPD

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


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.


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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消