【问题标题】:How can I post an image that is uploaded using <input type="file" /> without using the form's submit button? [duplicate]如何在不使用表单的提交按钮的情况下发布使用 <input type="file" /> 上传的图像? [复制]
【发布时间】:2013-03-04 02:42:21
【问题描述】:

这可以正常工作:

<form id="frm1" enctype="multipart/form-data" action="form_handler_post.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />     
    Send this file: <input name="userfile" type="file" />
    <input type="text" name="theRefNum" />
    <input type="submit" value="Send File" />
</form>

...但是我想使用 jQuery 发布表单的内容(即图像),因为我有很多其他数据要同时发布并且不想将其包含在与图片上传的形式相同。例如

$.post("form_handler_post.php", { name: "John", time: "2pm", otherData: "Friday", image:#######});

我尝试过image:userfileimage:'userfile'image:$('userfile').val(),但无济于事。

如何将图像文件包含在数据部分 - 即如何访问图像的value

【问题讨论】:

  • 没有简单的跨浏览器方式。如果您不需要 IE iframe 来模拟 Ajax。
  • @Bondye 序列化不会发送上传的文件数据。
  • @Bondye — 来自manual:“来自文件选择元素的数据未序列化”

标签: jquery html post


【解决方案1】:

你不能通过使用普通的 ajax 来做到这一点。您必须使用一些可用的插件。大多数插件在后端使用 iframe。 看到这个http://jquerybyexample.blogspot.com/2012/08/5-best-jquery-file-upload-plugin.html

【讨论】:

  • 完全不需要插件。这可以在a few small lines of code 中完成。
  • @JamesDonnelly 我接受你的 cmets。无论您在下面编写什么代码,插件也可以这样做。为什么我们不能使用可用的资源。大多数插件都经过了许多开发人员的测试和使用。
【解决方案2】:
$("input[name='userfile']").on("change", function(){
    readURL($(this).get(0));
})
function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('.somepicholder img').attr('src', e.target.result);
        }
        reader.readAsDataURL(input.files[0]);
    }
}

【讨论】:

  • OP 想要发布图片,而不是在同一页面中显示。
  • 上面的代码会做什么?
  • 从文件输入中读取图像源并使用data:base64将其放置在某个图像中
【解决方案3】:

您需要将文件提交到 iframe,因为您无法在 jQuery 中通过 AJAX 提交文件:

$('#frm1 input[type="submit"]').click(function(e) {
    /* Prevent this from submitting the form */
    e.preventDefault();

    /* Create an iframe. */
    var iframeName = "iframe" + (new Date()).getTime(),
        iframe = $('<iframe id="' + iframeName  + '" name="' + iframeName  + '"></iframe>');

    /* Add the iframe to the page and hide it. */
    $(this).append(iframe);
    $(iframe).hide();

    /* Set the form's target to point to the iframe. */
    $(this).attr({
        "action": "mypage.ext",
        "method": "post",
        "enctype": "multipart/form-data",
        "encoding": "multipart/form-data",
        "target": iframeName  
    });

    /* Call the modified form's submit function. */
    $(this).submit();
})

此代码在页面上创建一个 iframe,并确保它具有唯一的 ID 和名称。然后它将表单提交到隐藏的 iframe。

然后,您可以使用 iframe 的 onload 函数回调父页面(前提是它位于同一域中)。使用 window.parent.parentFunction("Successfully uploaded!") 之类的东西。

【讨论】:

  • 这可能会处理提交,但你忘了提到如果要使用响应数据,你需要在响应中放置一个&lt;script&gt;,并操作 DOM/访问 jQuery 使用window.parent。附言。从 jQuery 1.6 开始,.prop() 被推荐超过 .attr
  • 在您发布此内容时,我正在编辑我的答案。另外我相信attr() 更适合这里,因为它处理的是字符串而不是布尔值。
  • 当你操作 DOM 时,.prop() 更好,因为它设置了 DOM 元素的属性。使用.attr,您将强制使用.setAttribute(),这反过来将导致更新属性。差别不大,但 prop 在概念上是一种更好的工作方式,因为真正重要的是元素的属性。
猜你喜欢
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 2011-04-02
  • 2013-12-11
相关资源
最近更新 更多