C# Chrome中的上载IO错误
我正在尝试使用Uploadify创建一个基本示例,我的代码可以在除Chrome之外的所有浏览器中运行 基本上,我所要做的就是让用户选择一个嵌入页面的图像。用户选择一个文件,并在选择一个文件后,通过Uploadify将该文件发送到我的C#handler,该处理程序将图像转换为base-64编码字符串,并将其发送回目标C# Chrome中的上载IO错误,c#,javascript,jquery,io,uploadify,C#,Javascript,Jquery,Io,Uploadify,我正在尝试使用Uploadify创建一个基本示例,我的代码可以在除Chrome之外的所有浏览器中运行 基本上,我所要做的就是让用户选择一个嵌入页面的图像。用户选择一个文件,并在选择一个文件后,通过Uploadify将该文件发送到我的C#handler,该处理程序将图像转换为base-64编码字符串,并将其发送回目标img的src 这是我的JS: <link rel="stylesheet" href="Content/Uploadify/uploadify.css" /> <
img的src
这是我的JS:
<link rel="stylesheet" href="Content/Uploadify/uploadify.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="Content/Uploadify/jquery.uploadify.js"></script>
<script type="text/javascript">
$(function () {
$("#fileUpload").uploadify({
'swf': 'Content/Uploadify/uploadify.swf',
'uploader': 'ImageHandler.ashx',
'onUploadSuccess': function (file, data, response) {
$("#theImage").attr("src", data);
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
});
</script>
<input type="file" id="fileUpload" />
<img id="theImage" height="300" width="300"/>
正如您所看到的,它非常简单,在FF、IE(甚至IE 5模拟器w/IE 11!)、Safari中都可以使用,但在Chrome(v.31.0.1650.63 m)中,onUploadError
函数会被命中,错误变量如下:
文件:[文件对象]
错误代码:-220
errorMsg:Error#2038
errorString:IO错误
我正在使用Uploadify的最新版本(昨晚刚从Uploadify.com下载,v.3.2.1)
以前有没有人见过这个,或者知道我做错了什么
更新:
<link rel="stylesheet" href="Content/Uploadify/uploadify.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="Content/Uploadify/jquery.uploadify.js"></script>
<script type="text/javascript">
$(function () {
$("#fileUpload").uploadify({
'swf': 'Content/Uploadify/uploadify.swf',
'uploader': 'ImageHandler.ashx',
'onUploadSuccess': function (file, data, response) {
$("#theImage").attr("src", data);
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
});
</script>
<input type="file" id="fileUpload" />
<img id="theImage" height="300" width="300"/>
在做了一些谷歌搜索之后,似乎有些用户已经在Chrome中禁用了Flash,我可以验证这是否有效,但我不喜欢将其作为解决方案。如果转到Chrome插件页面,将安装两个版本:
如果我禁用列表中的第一个,我的Uploadify工作正常,但我不希望我的用户必须这样做
解决方案:
<link rel="stylesheet" href="Content/Uploadify/uploadify.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="Content/Uploadify/jquery.uploadify.js"></script>
<script type="text/javascript">
$(function () {
$("#fileUpload").uploadify({
'swf': 'Content/Uploadify/uploadify.swf',
'uploader': 'ImageHandler.ashx',
'onUploadSuccess': function (file, data, response) {
$("#theImage").attr("src", data);
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
});
</script>
<input type="file" id="fileUpload" />
<img id="theImage" height="300" width="300"/>
由于我使用Uploadify的全部目的是将图像发送给处理程序,并在不刷新页面的情况下使用处理程序的响应,并且该处理程序仅将图像转换为base64编码字符串,因此我将在可用的情况下使用HTML5的FileReader
。因此,对于Chrome、FF、IE 10&up,甚至不会使用Uploadify。以下是我的新代码,可以跨浏览器工作:
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
byte[] bytes = null;
using (var binaryReader = new BinaryReader(context.Request.Files[0].InputStream))
{
bytes = binaryReader.ReadBytes(context.Request.Files[0].ContentLength);
var base64 = Convert.ToBase64String(bytes);
var imgSource = "data: " + context.Request.ContentType + ";base64," + base64;
context.Response.ContentType = "text/plain";
context.Response.Write(imgSource);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("");
}
目前的解决方案是使用Modernizer来检测HTML5文件API是否可用(特别是文件阅读器
)。如果可用,我将使用FileReader
将图像转换为base 64编码字符串,并在img
的src
属性中使用该字符串
$(function () {
if (Modernizr.filereader) {
var $fileUpload = $("#fileUpload");
$fileUpload.on("change", function (e) {
var files = e.target.files;
if (files.length) {
var reader = new FileReader();
reader.onload = function (e) {
$("#theImage").attr("src", reader.result);
}
reader.readAsDataURL(files[0]);
}
});
} else {
// browser doesn't support the HTML 5 file reader api, so fall back to Uploadify:
$("#fileUpload").uploadify({
'swf': 'Content/Uploadify/uploadify.swf',
'uploader': 'ImageHandler.ashx',
'onUploadSuccess': function (file, data, response) {
$("#theImage").attr("src", data);
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
}
});
}
});
你在使用这个本地主机吗?Chrome在某些操作方面对localhost有限制,也许上传就是其中之一?如果是这样的话,那么一旦在托管部署上运行此操作,问题可能就会消失。我现在正在使用localhost。让我把它放在一个IIS网站上,然后快速试用。谢谢你的主意!不。仍然不起作用-同样的错误。除非Chrome对本地站点和本地主机都有限制?(我现在通过机器名而不是本地主机访问我的站点)恐怕我误导了你。根据这篇文章:,Uploadify似乎使用了一个flash组件,在ChromeSee my update中不起作用。它应该在铬合金中工作。。这似乎与Flash版本有关。我建议您在答案中编写解决方案(解决方案后面的部分:…),因为它看起来更符合Stack Overflow中的Q&A格式。顺便说一句,很高兴你解决了这个问题。打电话很好,我通常是这样做的,但昨天有点匆忙。再次感谢你的帮助。