Curl - 如何在浏览器获取文件名时获取文件名,但 Curl 不获取文件名?
Curl - How can I get the filename when browser does, but Curl doesnt?
我正在尝试执行简单的下载。我实际上使用了更长的循环并从文件列表中获取 URL,但这不是重点,因为无论哪种方式我都遇到了普通单数 Curl 查询的错误,所以我会尽可能简单。
当我尝试下载其中一个文件时:
curl "https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/" -O
我得到以下结果:
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application
但是,如果我将此 link 复制粘贴到我的 Edge 浏览器中并尝试保存文件,下载时会弹出一个名称:
15608770555178647552_screenshots_20220310005322_1.jpg
如何强制 Curl 获取此名称?我错过了什么?
我正在使用 Windows 10 和基本命令提示符 (cmd.exe)。
合并 -O
和 -J
尝试
curl -O -J "https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/"
If not told otherwise, curl writes the received data to stdout.
It can be instructed to instead save that data into a local file,
using the -o, --output or -O, --remote-name options. If curl is
given multiple URLs to transfer on the command line, it similarly
needs multiple options for where to save them.
不幸的是,在撰写本文时,curl 在 Content-Disposition 中不支持 UTF-8 文件名(参见 https://github.com/curl/curl/issues/1888 ), meaning you'll have to parse it out yourself.. if you have php-cli installed (see https://windows.php.net/ ),您可以
echo https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/ | php -r "if(0)var_dump(stream_get_contents(STDIN)); $ch=curl_init();$name='';curl_setopt_array($ch,array(CURLOPT_RETURNTRANSFER=>1,CURLOPT_HEADERFUNCTION=>function($ch,string $h)use(&$name):int{$pos=strpos($h,'filename*=UTF-8');if($pos!==false){$name=trim(substr($h,$pos+17,-3));}else{if(0)var_dump($h);} return strlen($h);},CURLOPT_URL=>trim(stream_get_contents(STDIN)) ));$bin=curl_exec($ch);file_put_contents($name,$bin);"
...但祝你好运,只用 curl/cmd.exe..
我正在尝试执行简单的下载。我实际上使用了更长的循环并从文件列表中获取 URL,但这不是重点,因为无论哪种方式我都遇到了普通单数 Curl 查询的错误,所以我会尽可能简单。
当我尝试下载其中一个文件时:
curl "https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/" -O
我得到以下结果:
curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application
但是,如果我将此 link 复制粘贴到我的 Edge 浏览器中并尝试保存文件,下载时会弹出一个名称:
15608770555178647552_screenshots_20220310005322_1.jpg
如何强制 Curl 获取此名称?我错过了什么?
我正在使用 Windows 10 和基本命令提示符 (cmd.exe)。
合并 -O
和 -J
尝试
curl -O -J "https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/"
If not told otherwise, curl writes the received data to stdout. It can be instructed to instead save that data into a local file, using the -o, --output or -O, --remote-name options. If curl is given multiple URLs to transfer on the command line, it similarly needs multiple options for where to save them.
不幸的是,在撰写本文时,curl 在 Content-Disposition 中不支持 UTF-8 文件名(参见 https://github.com/curl/curl/issues/1888 ), meaning you'll have to parse it out yourself.. if you have php-cli installed (see https://windows.php.net/ ),您可以
echo https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/ | php -r "if(0)var_dump(stream_get_contents(STDIN)); $ch=curl_init();$name='';curl_setopt_array($ch,array(CURLOPT_RETURNTRANSFER=>1,CURLOPT_HEADERFUNCTION=>function($ch,string $h)use(&$name):int{$pos=strpos($h,'filename*=UTF-8');if($pos!==false){$name=trim(substr($h,$pos+17,-3));}else{if(0)var_dump($h);} return strlen($h);},CURLOPT_URL=>trim(stream_get_contents(STDIN)) ));$bin=curl_exec($ch);file_put_contents($name,$bin);"
...但祝你好运,只用 curl/cmd.exe..