I want to pass a Tiff image to the web service, The webservice takes the Tiff Image as a parameter and I have to some operations on the Tiff Image.
Please suggest me a good way to pass Tiff Image as a parameter to the web service.
Thanks in advance.
enctype="multipart/form-data"
. HttpFileCollection files = HttpContext.Current.Request.Files;
in the web service (the action
attribute of the form) to get access to the uploaded file: HttpPostedFile file = files["id_of_field_with_filename"];
. Here "id_of_field_with_filename" is the id attribute of the <input>
tag of the HTML form which contains the filename. file.SaveAs(someDirectory + Path.GetFileName(file.FileName));
Note that in this solution the method of the web service (the "action") does not have any parameters. It is also possible to load the image using the HTML5 File API, convert it to base64 with JavaScript, and upload this string by an AJAX POST to the web service. In this case the method of the web service need to have a string parameter to receive the base64-encoded bytes of the image.
A web service can either accept a byte[]
or a Stream
as an input parameter.
This is an example of how you would pass in the Tiff file as a byte[]:
byte[] fileBlob = new byte[FileUploadControl1.PostedFile.InputStream.Length];
FileUploadControl1.PostedFile.InputStream.Read(fileBlob, 0, (int)FileUploadControl1.PostedFile.InputStream.Length);
svc.UploadFile(fileBlob);
This example is based on C# ASP.NET.
I'd do it the following way:
//get image from file or smth.
Image img = Image.FromFile(filename);
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, ImageFormat.Tiff);
bytes = ms.ToArray();
}
string ret = Convert.ToBase64String(bytes);
return ret;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.