【问题标题】:how to save data after download image?下载图片后如何保存数据?
【发布时间】:2017-03-30 09:21:49
【问题描述】:

我面临一个问题,我想在下载图像后保存数据。我的图像已成功下载,之后我想维护下载历史。这是我的功能:-

enter code here
public function UserImageDownlaod($userid=null,$imageid=null){
    $imagedata = DB::table('alt_images')->where('id',$imageid)->first();
    $imagedata = json_decode(json_encode($imagedata),true);
    $destination = 'images/ContributorImages'.'/';
    $pathToFile = $destination.$imagedata['img'];
    return response()->download($pathToFile);  
    //Now i want to save the history of downloaded images
    $history= new DownloadHistory;
    $history->user_id = Auth::user()->id;
    $history->alt_image_id = $imageid;
    $history->save();
 }

现在您可以在 return response()->download($pathToFile) 行之后看到我想保存历史数据。我希望当用户单击“确定”按钮时,数据将保存在历史记录表中。谁能帮我。

【问题讨论】:

  • 这是默认的浏览器弹出窗口。我们无法在代码中获取它的事件。
  • 如果我在响应下载之前保存它会保存但如果用户点击取消按钮。那么数据将被保存它会给我们带来问题
  • 你不能把return语句放在函数的末尾
  • 如果我输入结束,我知道我的数据将被保存,但如果用户点击取消,它也会将数据保存在历史表中
  • 你为什么不做类似的事情 - 当用户点击任何下载按钮时,会通过 javascript 生成一个弹出窗口,其中包含“你确定要下载图像”之类的消息。然后你会在弹出窗口中同时看到 OK 和 CANCEL 按钮。然后,如果用户单击“确定”,则将其视为您的用户想要下载图像,否则不要执行下载图像代码。

标签: php jquery laravel-5.2


【解决方案1】:

您向我们展示的弹出窗口来自浏览器。此时,您会看到此弹出窗口,浏览器可能已经开始在后台下载文件。由于这是您浏览器的一部分,并且您没有任何关于按确定/取消的回调,因此无法直接确定用户做了什么。

一个简单的解决方法是,在 javascript 中向用户显示一个警告框并询问他是否真的想要下载文件。

<a href="{{ action(...) }}" onclick="confirm('are you sure?')">Download</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多