普通网友 2017-03-06 13:27
浏览 50

从使用ajax调用的php函数下载图像

I have this ajax:

 $('.getid').on('click', function(){
        var imagename =  $(this).data("id");
        // you can make ajax call here to get data
        $.ajax({
        type: "POST",
        url: "Monitor/downloadDesiredImage",
        data: {'val' : imagename},
        dataType: "text"
        }).done(function(data) {
       console.log(data);        
       }).fail(function() {
    alert( "error" );
  });
});

This will work with a button that will send on.click a id value saved in a var imagename, that is a string that correspond to a image name saved on my server.

This is the php function that will get that id :

public function downloadDesiredImage() {
        $imagename = $_POST['val'];
        echo "this is my ".  $imagename;
        $file = file_get_contents('./images/'.$imagename.'.jpg', FILE_USE_INCLUDE_PATH);
        $imageData = base64_encode(($file));
        $src = 'data: '.mime_content_type($file).';base64,'.$imageData;
        echo '<img src="' . $src . '">';

    }

That will output the image in the network response, but I will also get the error :

Message: mime_content_type(): Invalid path

But what I want to do is after I click the button is to download that image from my server. How I can do that ? Thanks!

  • 写回答

1条回答 默认 最新

  • duandang6111 2017-03-06 13:31
    关注

    mime_content_type() expects the file name, not the data of the file itself. Something like this:

    mime_content_type('./images/'.$imagename.'.jpg')
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度