dowy77780 2014-09-10 16:05
浏览 58
已采纳

如何使用JavaScript将文件上载路径附加到表单?

Below is some jQuery/JavaScript code I have that handles file uploads on a Form I have. The user can upload as many files as they need to, after they upload a file, I show a preview image of the uploaded file in a Div.

What I need help with, is I need to somehow add the files that are uploaded, to a hidden form field so when they submit the Form, it will post the file name/path of each file that was uploaded with the other Form data to my backend PHP script.

I'm just not sure on the best way to do this?

I was thinking something like this might work...

<input type="text" name="uploads[]" id="uploads" value="FILE PATH HERE">

But i'm not sure if this would overwrite previous values each time a new file was added if there is more then 1 upload?

Any ideas on how to handle this? All I really need is to access the filename of each uploaded file in the backend once the Form is submitted.

jQuery(function () {
    jQuery('#fileupload').fileupload({
        url: '<?php echo Mage::getUrl('signwizard/index/saveFile') ?>',
        sequentialUploads: true,
        dataType: 'json',
        dropZone: jQuery('#dropzone'),
        pasteZone: jQuery(document),
        autoUpload: true,

        done: function (e, data) {
            jQuery.each(data.result.files, function (index, file) {
                jQuery('<div/>').html('<img src="/channelUploads/thumbnail/'+file.name+'"> '+file.name).appendTo(jQuery('#imageContainer'));


                // Add Uploaded image path and name to a Form field so thatr the file path will be posted when the Form is submitted.....

            });
        }
    });
});
  • 写回答

1条回答 默认 最新

  • douyan2821 2014-09-10 16:15
    关注

    To elaborate on my comment, as long as you append a new input for each file, you are good to go:

    done: function (e, data) {
        jQuery.each(data.result.files, function (index, file) {
             jQuery('<div/>').html('<img src="/channelUploads/thumbnail/'+file.name+'"> '+file.name).appendTo(jQuery('#imageContainer'));
    
             jQuery('#yourformid').append('input type="hidden" name="uploads[]" value="'+file.name+'">');
    
         });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable