【问题标题】:Preserve Content-Type when uploading a file from another URL without saving locally从另一个 URL 上传文件而不在本地保存时保留 Content-Type
【发布时间】:2021-02-18 08:44:46
【问题描述】:

鉴于许多文件(或一个大文件),我想从一台服务器下载并上传到另一台服务器,而不在本地存储文件。现在,我正在使用:

wget -q -O - <source> | curl --silent --show-error --fail -X PUT -d @- <destination>

但这会使用默认Content-Type: application/x-www-form-urlencoded 上传文件。有没有办法保留原来的 Content-Type?

【问题讨论】:

    标签: curl wget


    【解决方案1】:

    使用 curl 只需设置标题,例如,-H 'Content-Type: text/html':

    cat 1.html | curl --silent --show-error --fail -X PUT -H 'Content-Type: text/html' -d @- localhost:8000
    

    在你得到的服务器上:

    Content-Type: text/html
    

    通常(与浏览器一样),文件上传需要 method=post 和 enctype="multipart/form-data"。然后可以为每个部分设置 Content-Type。使用 curl 你可以这样做:

    curl ... -F "file=@-;type=text/html" <destination>
    

    在您将获得的服务器上:

    Content-Type: multipart/form-data; boundary=------------------------2fa1094be9afdaa6
    ...
    --------------------------2fa1094be9afdaa6
    Content-Disposition: form-data; name="file"; filename="-"
    Content-Type: text/html
    ...
    

    您需要从 wget 标头中提取相关的内容类型。也许通过使用 --save-headers 选项?首先将内容读入变量file,将标头字段值提取到变量content_type,然后将除标头之外的所有内容传递给curl,您现在可以使用上面的$content_type 调用它:

    wget ... | { file=$(</dev/stdin); content_type=$(echo "$file" | sed -n '1,/^\r$/ { /^Content-Type: /{ s/.*: \(.*\)/\1/p; q } }'); echo "$file" | sed '1,/^\r$/d' | curl ... }
    

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多