how to send a input file type via javascript in joomla modules

与世无争的帅哥 提交于 2019-12-23 12:32:21

问题


I want to send a file with javascript to php file .
I have this form in my php file

<form action="" method="post" enctype="multipart/form-data">
   <label for="file">Filename:</label>
   <input type="file" name="file" id="file1"><br>
   <p id="poi">upload</p>
   <p id="plo"></p>
</form>

and white this code in js file

jQuery(document).on("click","#poi",function(){
   var objfile=new FormData();
   var file=jQuery("#file1").get(0).files[0];
   objfile.append("iefile078",file);

   var ajax ;
   ajax.upload.addEventListener("progress" ,progresshandler ,false);
   function progresshandler(){
      jQuery("#plo").text("uploading ....");
   }
   ajax.open("POST","helper.php");
   ajax.send(objfile);
});

when I click on the "upload" on my page this function fired correctly.
I want to when upload is progressing "uploading .... " show to user.
additionally I want to send this file to helper.php file.
how to set attribute to the open() and send() function in this case to passing file uploaded to the helper.php?
this is my file structure and my form place in the default.php

js
    jquery.js
tmpl
    default.php
helper.php
mod_upload.php
mod_upload.xml

回答1:


Try this:

$(document).on("click","#poi", function(e) {
    e.preventDefault();
    var formData = new FormData($(this)[0]);

    $.ajax({
        url: 'helper.php',
        type: 'POST',
        data: formData,
        async: false,
        beforeSend: function() {
            $("#message1").show().html("uploading...");
        },
        success: function(data) {
            $("#message1").fadeOut();
            $("#container").html(data);
        },
        cache: false,
        contentType: false,
        processData: false
    });
});


来源:https://stackoverflow.com/questions/20660275/how-to-send-a-input-file-type-via-javascript-in-joomla-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!