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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值