简体   繁体   English

Ruby-open-uri本身不会下载文件,而只会下载网站的HTML代码

[英]Ruby - open-uri doesn't download a file itself, but just the HTML code of the website

I am trying to use this snippet: 我正在尝试使用以下代码段:

  open("data.csv", "wb") do |file|
    file << open("https://website.com/data.php", http_basic_authentication: ["username", "password"]).read
  end

But instead of the desired CSV file, I get just downloaded the HTML code of the website. 但是,我没有下载所需的CSV文件,而是下载了该网站的HTML代码。 What's the problem? 有什么问题? When I access the URL and I am not logged in, then it's displayed the form for login (not the HTTP authentication window). 当我访问URL且未登录时,它会显示登录表单(不是HTTP身份验证窗口)。 How to solve this situation? 如何解决这种情况?

Thanks 谢谢

我认为您应该尝试net / http http://dzone.com/snippets/how-download-files-ruby

It's probably because your php script return a response with a mime-type different of text/plain or better : text/csv 这可能是因为您的php脚本返回的响应类型为text / plain或更佳的mime类型:text / csv

please see this related previous response 请参阅此相关的先前回复

How to use the CSV MIME-type? 如何使用CSV MIME类型?

in the PHP Script : 在PHP脚本中:

header('Content-Type: text/csv');
header('Content-disposition: attachment;filename=data.csv');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM