douzhoulei8959 2012-09-12 22:29
浏览 41
已采纳

jQuery自定义文件输入插件 - 不上传的文件

** FOR FUTURE READERS **

Although I accepted an answer, this problem is not resolved for me. User dbf obviously knows his stuff and perhaps his answer will work for you. If it doesn't, I apologize. The waters are too deep for me, and I'm out of time to spend on this, so I have reworked my code to remove the custom-file-input niceties. I'll get my project completed and then, if possible, return to this code and post my solution. At the end of the day, however, user dbf put in a lot of effort and deserves the cred.


An ajax call returns a table structure, which gets placed inside form tags.

An ADD ROW button clones a table row, with a link (custom-file-input as anchor tag) for uploading a file.

I am replacing the INPUT TYPE=FILE element with the jquery-custom-file-input.js plugin, from here. This jquery plugin allows to turn any element into a file input element, so I am using an anchor tag #change_pd as the file input element. But to actually upload the file, the minimal documentation says to "attach the input element to a form element."

This StackOverflow question addresses exactly this issue, but I haven't been able to make it work in my case.

A standard submit button submits the form, where everything is received except the uploaded file.


HTML - An AJAX-generated table is inserted inside DIV #reportstable

<form action="" method="post" name="upd" enctype="multipart/form-data" id="AFrm">
    <div id="reportstable">
    </div>
</form>

AJAX - the returned table looks like this. The table is placed within div #reportstable, above.

<table id="DocTable">
<tr>
    <td>New Document</td>
    <td>
        <span id="span_pd"></span>
        <input type="hidden" id="proj_id" name="proj_id" value="'.$project_id.'">
        <input type="hidden" id="status_pd" name="status_pd">
    </td>
    <td>
        <a id="change_pd" href="#">change</a>
        <input type="hidden" id="new_pd" name="new_pd">
    </td>
</tr>
</table>

JQUERY - This is the code to insert a file. It does put the chosen filename into the SPAN as text, and into the hidden input file #new_pd, which is received in the $POST data.

However, the file itself is not uploaded.

I am not sure where to put the .appendTo(), or how to write the jquery. Here is what I tried. Everything works except the .appendTo().

$(document).on('click','#change_pd',function() {
    $(this).file().choose(function(e, input) {
        $('#new_pd').val(input.val());
        $('#span_pd').html(input.val());
        $('#span_pd').css('color','grey');
        $('#status_pd').val('CHANGED');
    });
    $(this).css('text-transform','uppercase');
    $(input).appendTo('#AFrm').
        attr('name', 'a-name').
        attr('id', 'an-id');
    });

PS - If anyone can recommend a better (preferred?) custom-file-input plugin, I'm all ears.

  • 写回答

1条回答 默认 最新

  • doubei3312 2012-09-12 22:49
    关注

    If you look at the code from the plugin you gave

        var file = $('<div><form></form></div>').appendTo('body').css({
            'position': 'absolute',
            'overflow': 'hidden',
            '-moz-opacity': '0',
            'filter':  'alpha(opacity: 0)',
            'opacity': '0',
            'z-index': '2'      
        });
    
        var form = file.find('form');
        var input = form.find('input');
    
        function reset() {
            var input = $('<input type="file" multiple>').appendTo(form);
            input.change(function(e) {
                input.unbind();
                input.detach();
                btn.trigger('choose', [input]);
                reset();
            });
        };
        reset();
    

    then you will see that the <form> and <input type="file" multiple> are appended to the body. This means that your form with id="AFrm" will not contain any <input type="file"> element and that's why you are not uploading any file but only receive the selected name of the file.

    You could adapt the plugin to append the input to a specific form given by a parameter as an option through an object like { 'form':$('#AFrm') }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler