weixin_33713350 2012-08-08 06:41 采纳率: 0%
浏览 21

通过AJAX传输图像

I'm creating a setup wizard using AJAX. It's a multi-step form submission with 6 total steps. Steps 1-5 work fine, they are just form fields and only submit text. The final step, step 6, will allow the user to upload 7 images. This step does not work. I get a 500 Internal Server Error

Am I passing the image data correctly via JSON? Is there something else I'm doing wrong or forgetting?

The relevant code is below:

HTML (For step 6 only)

<form action="/ajax/wizard.php/<?php echo $userName ?>?step=3" 
class="defaultRequest" enctype="multipart/form-data" method="post">

<input type="hidden" name="token" value="<?php echo $token; ?>"/>

<fieldset>
    <p><label>Profile Picture</label>
    <input type="file" name="pPic" value="" /></p>

    <p><label><a href="#help-username" class="show_helper"><span>(?)</span>
    Pic 1</a></label> <input type="file" name="Album1" value="" />
    </p>

    <p><label><a href="#help-password" class="show_helper"><span>(?)</span>
    Pic 2</a></label><input type="file" name="Album2" value="" />
    </p>

    <p><label>Pic 3</label>
    <input type="file" name="Album3" value="" /></p>

    <p><label>Pic 4</label>
    <input type="file" name="Album4" value="" /></p>

    <p><label>Pic 5</label>
    <input type="file" name="Album5" value="" /></p>

    <p><label>Pic 6</label>
    <input type="file" name="Album6" value="" /></p>

</fieldset>

<fieldset>
     <p><label>&nbsp;</label>
     <button type="submit"><span>Upload Images</span></button></p>
</fieldset>

JS

$.ajax({
type: 'POST',
url: requestUrl,
data: $(this).serialize(),
dataType: 'json',
success: function(data) {

            if(data.response){
                $('div.errormsg').remove();
                $(eventHeadline).html(data.eventHeadline);
                console.log(data.eventHeadline);
                //$(eventDate).html(data.eventName);

                if(data.step){
                    openStep(data.step);
                }else{
                        openStep('next');
                }
            }else{
                $('div.errormsg').remove();
                $('<div class="errormsg">'+data.message+"</div>").insertBefore(form);
            }
  • 写回答

1条回答 默认 最新

  • weixin_33724570 2012-08-08 06:50
    关注

    File fields don't serialize conveniently to JSON, in order to upload them you would need to create a FormData object which can be uploaded using jQuery so long as you prevent jQuery from processing the data object using processData: false. This will only work with a few of the newest browsers though: http://caniuse.com/#search=formdata

    In order to support file uploads with jQuery for older browsers / IE, your best bet is to find a plugin that uploads the files using a standard POST and ties it back to the jQuery callback - this should provide you with a few: https://www.google.co.uk/search?q=jquery+file+upload+plugin

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考