我有两个应用程序。其中之一是基于 HTML 的 Web 应用程序,它在 IE9 模式下工作,并通过 XDomainRequest 以 base64 字符串格式接收扫描文档的图像。
另一个应用程序是用 C# 编写的 Windows 服务。它通过 HttpListener 监听端口上的请求。然后它扫描文档并通过 HttpListenerResponse 发送扫描的图像。
扫描的图像将由客户端的 Web 应用程序处理,然后将它们上传到服务器。所以我必须在 javascript 中完成所有工作。没有服务器端解决方案(目前)。
如果我只收到一张图片,这不是什么大问题。但是如果发件人发送多张图片,base64 字符串会变得太大,需要太多时间。
我使用下面的代码将图像转换为 base64 字符串:
public static string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
下面的代码用于接收图像:
if (xdrScanning.responseText != "") {
try {
json = JSON.parse(xdrScanning.responseText);
$('#scannedImage').attr('src', 'data:image/png;base64, ' + json[0].BASE64IMAGE);
} catch (e) {
xdrScanningError();
}
}
据我所知,XDomainRequest 只接收字符串响应。
那么,我能做些什么来克服这个性能问题呢?