【发布时间】:2015-10-09 03:32:15
【问题描述】:
更新:
已解决: http://jsfiddle.net/TmUmG/230/原问题:
我有一个图像/徽标,在隐藏的输入 type=file 旁边:
<form class="image fit" id="theuploadform">
<input title="click me to change logo" type="image" src="images/sample_image.jpg" style="width:inherit;height:inherit"/>
<input type="file" id="userfile" name="userfile" style="display:none" />
</form>
想法是点击图片浏览文件,上传并刷新原始图片。我的代码在 IE 中工作,但在 Chrome/FF 中不工作((
点击处理:
// click for image upload
$("input[type='image']").click(function () {
$("input[id='userfile']").click();
});
这会提示我选择一个文件。在 onchange 之后,我想提交表单:
// upload image form
$("input[id='userfile']").on("change", function() {
//alert('here!');
var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none"></iframe>');
$("body").append(iframe);
var form = $('#theuploadform');
form.attr("action", "/UploadHandler.ashx");
form.attr("method", "post");
form.attr("encoding", "multipart/form-data");
form.attr("enctype", "multipart/form-data");
form.attr("target", "postiframe");
form.attr("file", $('#userfile').val());
form.submit();
$("#postiframe").load(function () {
iframeContents = this.contentWindow.document.body.innerHTML;
$("#textarea").html(iframeContents);
var filename = $('#userfile').val().replace(/\\/g, '/');
var fileNameIndex = filename.lastIndexOf("/") + 1;
filename = filename.substr(fileNameIndex);
$("input[type='image']").attr("src", "Uploads/" + filename);
});
//return false;
});
以上所有内容都适用于 IE 9+。
我刚刚发现了这个帖子: Upload files using input type="file" field with .change() event not always firing in IE and Chrome 目前还不确定,但听起来很相关。
【问题讨论】:
-
$("input[id='userfile']").click();而您的另一个活动是change?请使用相同的事件 -
是
form.attr("file", $('#userfile').val());File对象form属性file的预期结果? -
`
"Uploads/"$("input[type='image']").attr("src", "Uploads/" + filename);的目的是什么? -
我有一个处理程序:UploadHandler.ashx,它读取表单,将文件保存在服务器上的“Uploads”文件夹中。我正在引用保存图像的预期路径
标签: javascript jquery html google-chrome