views:

605

answers:

1

Hello! I have a problem with using binary content of file. I want to pass web method content of file. I retriev it from fileupload control on my page via javascript function getAsBinary(). But error appears in web method, when I try to create example of class Image. So, I have the page (.aspx) with fileupload control and scriptmanager. There are three javascript function:

// Get image from fileupload control and pass it in webmethod
function Get_image() {
    var file_uploader = document.getElementById(file_uploader_name);
    var file_content = file_uploader.files[0].getAsBinary();
    imupcon.Get_image(file_content, OnRequestComplete, OnError);
}
// Successful execution
function OnRequestComplete(result) {alert(result);}
//Error execution
function OnError() { alert("Error!");}

And I have web-service with web-method:

[WebMethod]
public string Get_image(string file_content, string file_name) 
{
 byte[] data = Encoding.Unicode.GetBytes(file_content);
 MemoryStream memStream = new MemoryStream();
 memStream.Write(data, 0, data.Length);

 //Error appears here
 System.Drawing.Image image = System.Drawing.Image.FromStream(memStream);

 memStream.Close();
 return "Hurray!";
}

Does any have idea, what is reason? How I can pass content of file to web method? Thanks.

+1  A: 

You don't need Encoding.Unicode.GetBytes if it's as it's already in binary. The data would be in unicode if you called files[0].getAsText("utf-8"). Please note that all of these methods are now obsolete and you should use feature detection and use the standard FileReader API if it is available.

Eli Grey
Nice. So what would OP do instead? `r = new FileReader(); r.onloadend = /*...*/; r.readAsBinaryString(file_uploader.files[0].name)`?
Crescent Fresh
But is it really to create example of class Image using file content, which is passed by js function?
greatromul
I barely understood your question and you didn't give me much info so I just pointed out what might be causing whatever problems you are encountering.
Eli Grey
I want to choose image file with help fileuploader, then I want to pass its content to web method and create image file on server in this web method. But when I try to create example of class Image or fill FIleStream with binary content of chosen file, error appears and it interrupts execution of my web method.
greatromul