简体   繁体   English

如何使用 AJAX 与其他 FormData 一起上传 .mp4、.mp3 和 .doc 文件?

[英]How can I upload .mp4, .mp3 and .doc Files along other FormData with AJAX?

Good day guys.美好的一天,伙计们。 I've been uploading files (docs and images) from AJAX.我一直在从 AJAX 上传文件(文档和图像)。 Although, I've searched this site for uploading videos from AJAX, I have not seen exactly what I want.虽然,我已经搜索了这个网站以从 AJAX 上传视频,但我还没有看到我想要的内容。 I only saw, uploading a live-stream video and that does not really help me.我只看到,上传一个直播视频,这并没有真正帮助我。

Now, I want to upload all three (docs, audio and video) from AJAX.现在,我想从 AJAX 上传所有三个(文档、音频和视频)。 The code below works well when I select only.DOC to upload but it does not work when I combine.DOC and.MP4 files.下面的代码在我只上传 select.DOC 时运行良好,但当我结合.DOC 和.MP4 文件时它不起作用。

var fdata = new FormData();
            fdata.append("cid", $("#tclass").val() ); fdata.append("subj", $("#tsubj").val() );
            fdata.append("sess", $("#sess").val() ); fdata.append("term", $("#term").val() );  
            fdata.append("title", $("#lessontitle").val() );
            
            if(  $("#lessontext").val() !== ""){ fdata.append("note", $("#lessontext").val() ); }
            fdata.append("video", $("#videofile")[0].files[0] );            
            fdata.append("doc", $("#docfile")[0].files[0] ); 
            fdata.append("audio", $("#audiofile")[0].files[0] );
            
            document.getElementById("msgStatus").innerHTML = "Uploading Lesson and Checking for Previously Uploaded Title";
            
            $.ajax({                    
                type: 'POST',
                url: "lessons/validateupload/",
                data:fdata,
                contentType: false,
                processData: false, 
                cache: false,               
                success: function(data){ alert(data);                       
                    $('#msgStatus').html(data);                     
                    if(data.indexOf("successfully") > -1){ 
                        closeForm();
                    }
                }       
            })

THE RESULT RETURNED FROM PHP WHEN I SELECT ONLY.doc当我只使用 SELECT 时,从 PHP 返回的结果.doc

Array
(
    [cid] => 1-JSS1A
    [subj] => 1-MATHEMATICS
    [sess] => 1
    [term] => 1
    [title] => INTEGRATION
    [note] => thhis is a mathematical concept...
    [video] => undefined
    [audio] => undefined
)
Array
(
    [doc] => Array
        (
            [name] => Design and Implementation of a Web.docx
            [type] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            [tmp_name] => C:\wamp64\tmp\php9185.tmp
            [error] => 0
            [size] => 711124
        )

)

AND THIS IS THE RESULT WHEN I COMBINE.doc FILE AND.mp4 FILE I get empty ARRAYs这是当我组合.doc 文件和.mp4 文件时的结果,我得到空 ARRAYs

Array
(
)
Array
(
)

When I try to upload only.mp4, I also get empty arrays当我尝试仅上传.mp4 时,我也得到空的 arrays

Array
(
)
Array
(
)

Please I need help.请我需要帮助。 I do not really understand what am doing wrong.我真的不明白做错了什么。 Thank you.谢谢你。

I have solved the problem.我已经解决了这个问题。 I had to edit my Php.ini file, increasing the post_max_size to 200. I also co figured my ajax post to upload the files in batches.我不得不编辑我的 Php.ini 文件,将 post_max_size 增加到 200。我还计算了我的 ajax 帖子以批量上传文件。 After uploading file 1, it will check the responseText in the eventCompleteHandler().上传文件1后,会检查eventCompleteHandler()中的responseText。 If it reruns success, then check if another file is selected and post it else check for another file and post it.如果它重新运行成功,则检查是否选择了另一个文件并发布它,否则检查另一个文件并发布它。 If no other files, then report.. Upload successful and completed.如果没有其他文件,则报告.. 上传成功并完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM