dongzhong1891 2017-03-30 05:40
浏览 31

使用ajax上传图像以提交表单需要花费大量时间

Basically I am using html form for uploading image on server. Below is my code for form:

<div class="modal modal-fixed-footer" id="modal-image">
<div class="modal-content">
    <h3>Upload Image</h3>
    <form id="uploadimg" enctype="multipart/form-data">
        <input type="hidden" name="entity" value="order">
        <input type="hidden" name="orderId" value="<?php echo $orderId; ?>">
        <input type="hidden" name="status" value="<?php echo $json->response->body->order->status; ?>" />
        <input type="hidden" name="url" value="order-details.php?order=<?php echo $orderId; ?>&e=0">
        <div class="form-inputs p-l-r-0">
            <div class="row">
                <div class="col s12">
                    <div class="file-field input-field" style="display:none">
                        <div class="btn accent-color">
                            <span>File</span>
                            <input type="file" id="fileToUpload" name="fileToUpload[]" multiple="multiple">
                        </div>
                        <div class="file-path-wrapper">
                            <input class="file-path validate" type="text" placeholder="Upload one or more images">
                        </div>
                    </div>
                </div>
            </div>
            <div id="dvPreview" class="row"></div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button class="btn-flat modal-action modal-close" id="uploadimg-submit">Submit</button>
    <button class="btn-flat modal-action modal-close">Close</button>
</div>

Now when the #uploadimg-submit is clicked below jquery code is executed:

$('#uploadimg-submit').on('click', function(e) {
                   e.preventDefault();
                   $("form#uploadimg").submit();
               });

and form submit function:

    $("form#uploadimg").submit(function(e2) {
                   var smoothState = $('#main').smoothState().data('smoothState');
                   e2.preventDefault();
                   var formData = new FormData($(this)[0]);
                   // console.log(formData);
                   $.ajax({
                       url: 'processors/process-upload.php',
                       type: 'POST',
                       data: formData,
                       processData: false,
                       contentType: false,
                       beforeSend: function() {
                           ajaxindicatorstart();
                       },
                       success: function(rsp) {
                           if (rsp == '200') {
                               Materialize.toast('Image Uploaded Successfully', 2000, 'green');
                               smoothState.clear('/order-details.php');
                               smoothState.load('/order-details.php?order=' + $('#order_id').val());
                           } else {
                               Materialize.toast('Image Uploaded Failed', 2000, 'red');
                               smoothState.clear('/order-details.php');
                               smoothState.load('/order-details.php?order=' + $('#order_id').val());
                           }
                       },
                       error: function() {
                           Materialize.toast('Image Uploaded Failed', 2000, 'red');
                           smoothState.clear('/order-details.php');
                           smoothState.load('/order-details.php?order=' + $('#order_id').val());
                       }
                   });
                   return false;
    });

The file process-upload.php takes less than a second to process the image which i debugged using microtime(true). But total time take by sending request and recieving response is nearly 11 secs, majority of which (about 9 secs) is taken in sending request. Below are timeline analytics from chrome dev tools. process-upload total time

content-length of ajax request

Request time

I want to reduce this time else it will be unfeasible to wait so long for uploading image. Is there any way I can optimize it further?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法