相关疑难解决方法(0)

如何异步上传文件?

我想用jQuery异步上传一个文件.这是我的HTML:

<span>File</span>
<input type="file" id="file" name="file" size="10"/>
<input id="uploadbutton" type="button" value="Upload"/>
Run Code Online (Sandbox Code Playgroud)

在这里我的__CODE__代码:

$(document).ready(function () {
    $("#uploadbutton").click(function () {
        var filename = $("#file").val();

        $.ajax({
            type: "POST",
            url: "addFile.do",
            enctype: 'multipart/form-data',
            data: {
                file: filename
            },
            success: function () {
                alert("Data Uploaded: ");
            }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

我只获取文件名,而不是上传文件.我该怎么做才能解决这个问题?

现行解决方案

我正在使用jQuery Form Plugin上传文件.

javascript ajax jquery asynchronous xmlhttprequest

2841
推荐指数
28
解决办法
129万
查看次数

当php脚本通过ajax运行时显示进度条

我有一个表单,通过ajax向服务器提交值.

<form>
<input id="titlee" name="titlee">
<input type="file" name="fileToUpload" id="fileToUpload">
<button  type="submit" value="submit" id="submit" name="submit">Start</button>
<div class="progress"></div>
</form>
Run Code Online (Sandbox Code Playgroud)

脚本

<script type="text/javascript">
$(function() 
    {
        $("#submit").click(function() 
            {
                var titlee = $("#titlee").val();
                var fileToUpload= $("#fileToUpload").val();

                var dataString = 'titlee='+ titlee + '&fileToUpload=' + fileToUpload;

                $.ajax(
                    {
                        type: "POST",
                        url: "c_insert_test.php",
                        data: dataString,
                        success: function()
                    });

                return false;
            });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

c_insert_test.php

   <?php
    $titlee = $_POST['titlee'];
    $target_dir = "reqdoc/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    $new_filename = $target_dir . uniqid() …
Run Code Online (Sandbox Code Playgroud)

javascript php mysql ajax jquery

6
推荐指数
0
解决办法
7396
查看次数

jQuery 的“uploadProgress”没有在“$.ajax”中触发

我是 jQuery 新手,现在我正在处理文件上传。我想在每次上传图片时添加一些进度条。我uploadProgress在 jQuery 中使用了,但它似乎不起作用。这是我的代码:

$('#_form_').on('submit', function(e){

   var file_and_desc = new FormData($(this)[0]),
       form_url = "_pages/_form_";

       var ext = choose.val(),
           allowed = ['jpg','png'];

       if(ext){
          var get_ext = ext.split('.');
              get_ext.reverse();

              if($.inArray(get_ext[0].toLowerCase(), allowed) > -1){
                   //upload image
                   $.ajax({
                         url : form_url,
                         type: 'POST',
                         data: file_and_desc,
                         contentType: false,
                         processData: false,
                         uploadProgress: function(event, positio, total, percentComplete){
                          $('h1').html(percentComplete);
                         },
                         success: function(data){
                              // some code here...
                         }
                   });
              }
       }
});
Run Code Online (Sandbox Code Playgroud)

就是这样!我该怎么办?

javascript jquery file-upload progress-bar

5
推荐指数
1
解决办法
8737
查看次数

jQuery.post动态数据回调函数

我有一个脚本,需要几秒钟的处理,最多约一分钟.该脚本调整图像数组的大小,锐化它们并最终将它们拉链以供用户下载.

现在我需要某种进度信息.我在想,使用jQuery的.post()方法,来自回调函数的数据会逐步更新,但这似乎不起作用.

在我的例子中,我只是使用循环来模拟我的脚本:

        $(document).ready(function() {
            $('a.loop').click(function() {
                $.post('loop.php', {foo:"bar"},
                function(data) {
                    $("div").html(data);                        
                });
                return false;
            });
        });
Run Code Online (Sandbox Code Playgroud)

loop.php:

for ($i = 0; $i <= 100; $i++) {
    echo $i . "<br />";
}
echo "done";
Run Code Online (Sandbox Code Playgroud)

javascript jquery callback http-post

3
推荐指数
1
解决办法
3850
查看次数