开发者

Upload file to ASP web service with HTTP/1.1 POST

开发者 https://www.devze.com 2023-02-19 11:12 出处:网络
I\'m trying to upload a file to an ASP 2.0 web service through HTTP 1.1 POST from a client-side application.If my web service function is declared as

I'm trying to upload a file to an ASP 2.0 web service through HTTP 1.1 POST from a client-side application. If my web service function is declared as

<WebMethod()>
Public Function UploadFile(ByVal file as Byte(), ByVal fileName as String) as String
...

The test form says th开发者_StackOverflow社区e request should take this form:

POST address_of_service HTTP/1.1
Host: <host>
Content-Type: application/x-www-form-urlencoded
Content-Length: length

file=string&file=string&fileName=string

How do I get my binary file data into that form? I've tried just converting the file data to a string and putting it in the body (file=<string_data_here>) but I get HTTP 500 errors back, complaining about not being able to convert it to System.Byte. I've got HTTP POST working fine elsewhere in my application with plain string parameters.

Also, out of curiosity, why is it showing the file parameter twice?


Convert the binary stream to a Base64 based string and pass it to the webservice using Post. When the webservice receives the call to execute you can then convert back from the Base64 string to the original binary array sequence.

See the following;

Converting the file to Base64 string to POST;

var fileStream = File.Open(<your file path>, FileMode.Open, FileAccess.Read);
var reader = new BinaryReader(fileStream);
var data = new byte[fileStream.Length];
reader.Read(data, 0, data.Length);
var strBase64 = Convert.ToBase64String(data, 0, data.Length);

Converting it back to binary in the SOAP webservice method:

var data = Convert.FromBase64String(<your webmethod input Base64 string>);

Disclaimer: I've provided this example without having the opportunity to test that it compiles but it should hopefully point you in the right direction to complete your requirement.

0

精彩评论

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