douxia9826 2014-09-22 13:16
浏览 30
已采纳

通过ajax将多个数据(如图像文件)发布到php

I have some trouble sending the following html input type to my php script through ajax. I'm guessing that I have to define the file in tje js code hoverver how to do so when I have more variables that are taking information from the same file (se php code)?

<input id="imagefile" class="file" type="file" name="image" />

through this code

$("#addmedia").click(function(ev) {
            ev.preventDefault();
            var p                   = $("#p").val();
            var mediatype           = $("#mediatype option:selected").val();
            var addmediatype        = $("#mediatype option:selected").val();
            var title               = $("#title").val();
            var video               = $("#medialink").val();
            var imagefile           = $("#imagefile").val();

            $.post("lib/action.php", {
                mediatype:      mediatype,
                addmediatype:   addmediatype,
                title:          title,
                video:          video,
                addmedia:       true
            }, function(data) {
                $("#notify").hide().html("<h1>!</h1><h2>" + data + "</h2>").slideDown(500);
                setTimeout(function() { $("#notify").slideUp(500) }, 2500);
            });
        });

so that it works with my php upload script.

In my php code i use following variables to get infro from the file

if( $_POST['p'] == 1 ) {
            $name =     $_FILES['image']['name'];
            $temp =     $_FILES['image']['tmp_name'];
            $type =     $_FILES['image']['type'];
            $size =     $_FILES['image']['size'];

(...)
}
  • 写回答

1条回答 默认 最新

  • dsgtew3241 2014-09-22 13:31
    关注

    When you use $().val for a file field, you're only getting the filename because of security restrictions.

    One solution (for IE 10+, Chrome, FF) is to read the file contents using https://developer.mozilla.org/en-US/docs/Web/API/FileReader, base64 encode it and upload it. See Reading client side text file using Javascript

    document.getElementById('file').addEventListener('change', readFile, false);
    
    function readFile (evt) {
       var files = evt.target.files;
       var file = files[0];           
       var reader = new FileReader();
       reader.onload = function() {
          console.log(this.result);            
       }
       reader.readAsText(file)
    }
    

    Note that there are too many gotchas when uploading files through AJAX and can't possibly address with a concise answer as StackOverflow answers should be.

    The easiest workaround is to not send it using AJAX, use a regular form upload, but target a hidden iframe so your page doesn't reload.

    See:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线