当文件输入中存在名称字段时,getElementById 不会发布


getElementById does not post when name field is present in the file input

我正在编写一个代码,只需一步即可浏览和上传文件。我有这段代码:

<input type="file" id="image" style="display: none;">
<input type="submit" name="upload" id="upload" value="upload"
  onclick="document.getElementById('image').click();">

此代码允许我选择文件并提交表单,但我想使用 $_FILE 属性,并且由于文件输入中没有名称字段,我无法访问文件信息。

所以我修改了我的代码以添加名称字段,如下所示

<input type="file" name="image" id="image" style="display: none;" />
<input type="submit" name="upload" id="upload" value="upload"
  onclick="document.getElementById('image').click();" />

现在,使用此代码,我仍然可以浏览该文件,但它没有提交。因此,简而言之,我无法使用这两种方法获得 _FILE 美元。

任何建议我能做些什么来前进。 ??

蒂亚

要在 PHP 的 $_FILES 数组中提供所选图像,您需要为文件字段指定一个 name 属性,并将表单的enctype设置为multipart/form-data以便浏览器正确发布。

<form action="some.php" method="post" enctype="multipart/form-data">
 <input type="file" id="image" name="image">
 <input type="button" value="select image" id="button">
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

为了能够在文件输入上仍然使用 .click() 方法,它必须没有从文档中删除(即:display: none不起作用),因此您可以使用:

​input[type=file] {
 position: absolute; height: 0; width: 0; /* should be totally squashed */
 visibility: hidden; /* just in case */
}​

然后,您可以使用JavaScript打开选择对话框并提交表单

$('#button').click(function() {
  $('#image')[0].click();
});​​​​
$('#image').change(function() {
  $('form').submit();
});

(这里假设你的页面上有jQuery,这里有一个工作示例 http://jsfiddle.net/steveukx/V4yE3/)

虽然我在这里不支持您的方法(它不会将标记与行为分开),但您可以尝试在 onclick 中添加一个return true

此代码无法跨浏览器工作。在 chrome 上,您无法对隐藏的input[type=file]元素调用click()

即使将其切换为可见,将click()放入onclickonsubmit处理程序中也不起作用 - 表单将不会与新文件值一起提交。这是一项安全功能。浏览器不希望您这样做。

Flash可以做你想做的事。查看 YUI 上传器组件:http://developer.yahoo.com/yui/uploader/

如果您想在不重新加载页面的情况下上传文件,请使用 AJAX 文件上传 jQuery 插件会以某种方式上传文件,并将响应传递给回调,仅此而已。

It does not depend on specific HTML, just give it a <input type="file">
It does not require your server to respond in any particular way
It does not matter how many files you use, or where they are on the page
--

少则使用 --

$('#one-specific-file').ajaxfileupload({
  'action': '/upload.php'
});
——

或者和——一样多——

$('input[type="file"]').ajaxfileupload({
  'action': '/upload.php',
  'params': {
    'extra': 'info'
  },
  'onComplete': function(response) {
    console.log('custom handler for file:');
    alert(JSON.stringify(response));
  },
  'onStart': function() {
    if(weWantedTo) return false; // cancels upload
  },
  'onCancel': function() {
    console.log('no file selected');
  }
});

我了解,您只想a)隐藏浏览器特定的文件浏览控件并提供您自己的样式按钮和b)同时选择和上传文件(例如,在文件浏览对话框关闭时上传您的照片)。

在这种情况下,您需要这样的东西:

<form name="myForm" method="POST">
    <input type="file" name="image" id="image" 
        style="position: absolute; left: -1000px;"
        onchange="myForm.submit();" />
    <input type="button" name="upload" id="upload" value="upload"
        onclick="document.getElementById('image').click();" />
</form>

适用于Chrome(不要隐藏文件输入,而是将其移出页面)。如果其他浏览器有任何问题,我建议使用jQuery。

呵呵

<input type="file" id="image" onchange="yourForm.submit();">