0

我在使用文件的二进制内容时遇到问题。我想传递文件的网络方法内容。我通过 javascript 函数 getAsBinary() 从页面上的文件上传控件中检索它。但是当我尝试创建类 Image 的示例时,Web 方法中出现错误。因此,我拥有带有文件上传控件和脚本管理器的页面 (.aspx)。有三个javascript函数:

// 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!");}

我有网络方法的网络服务:

[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!";
}

有没有人知道,原因是什么?如何将文件内容传递给 Web 方法?谢谢。

4

1 回答 1

1

Encoding.Unicode.GetBytes如果它已经是二进制文件,则不需要。如果您调用,数据将采用 unicode 格式files[0].getAsText("utf-8")。请注意,所有这些方法现在都已过时,您应该使用特征检测并使用标准 FileReader API(如果可用)。

于 2009-11-30T18:31:42.650 回答