Javascript 为什么我的ajax文件上传程序不能正常工作?

Javascript 为什么我的ajax文件上传程序不能正常工作?,javascript,ajax,dom,file-upload,Javascript,Ajax,Dom,File Upload,我正在尝试创建一个ajax文件上传模块。请看下面的代码片段 //create the form for uploading the file the ajax file FileUploader.prototype.createForm = function() { // create the new form var form = document.createElement('form'); form.id = this.form_id; form.actio

我正在尝试创建一个ajax文件上传模块。请看下面的代码片段

//create the form for uploading the file the ajax file
FileUploader.prototype.createForm = function() {
    // create the new form
    var form = document.createElement('form');
    form.id = this.form_id;
    form.action = this.url;
    form.method = 'post';
    form.enctype = 'multipart/form-data';
    form.target = 'file_upload_iframe';

    // try to create a file type of input failed at [1]
    /* var input_file = document.createElement('input');
    input_file.type = 'file';
    input_file.name = this.name;
    input_file.value = this.file;  [1] */

    // try to clone the input file but failed to insert it to the old form[2] 
    // or the new form [3] either
    var input_file = document.getElementById('userfile');
    var new_input_file = document.cloneNode(true);
    // document.getElementById('file_upload').appendChild(new_input_file); [2]
    new_input_file.id = ''; 

    form.appendChild(new_input_file); // [3]
    document.body.appendChild(form);
    return form;
};
  • 为什么在[1]的地方我得到了一个安全错误
    security error”code:“1000
    ,你能提供一个参考来源吗

  • 为什么我不能将新的输入文件附加到新创建的表单[3]中,甚至不能将新克隆的新输入文件附加到旧表单([2])


  • 谢谢。

    您不能使用jquery操作DOM吗

    var input = $('<input>'); // create your element
    input.append($('#userfile').clone()); // append something
    
    var输入=$('');//创建您的元素
    input.append($('#userfile').clone());//附加某物
    
    您试图克隆整个文档,但这毫无意义。您需要的是这样的代码:

    var new_input_file = input_file.cloneNode(true);
    
    无论如何,出于明显的安全原因,输入类型文件的
    值是只读的,因此“克服”这一问题的唯一方法是将实际输入添加到新表单中:

    form.appendChild(input_file);
    

    并且应该保留该文件。(没有测试)

    谢谢。哦,我应该重新检查代码,其实我以为它是输入文件而不是文档,然后花了半个小时找到了不存在的答案。在我的笔记本前设置了将近8个小时后……我的脑袋乱了……:)