【问题标题】:Trying to use ftplib to upload an image but just getting a blank file any ideas?尝试使用 ftplib 上传图像但只是得到一个空白文件有什么想法吗?
【发布时间】:2012-11-15 15:33:39
【问题描述】:

这是我的代码

import ftplib

s = ftplib.FTP("Host", "Username", "Password")
s.cwd("public_html/test")
image = open("test.jpg", "rb")
s.storbinary('STOR test.jpg', image) 
image.close()
s.quit()

当我检查它时,我只是得到一个损坏的图像......我已经到了我将对图像进行 base64 并上传的地步......

如果有人可以帮忙,请告诉我我做错了什么

【问题讨论】:

  • 如果您可以进行二进制差异并描述 如何 图像损坏可能会有所帮助。它是零字节吗?它是否正确,但其余部分缺失?它大部分是正确的,但有几个字节是错误的吗?尺寸是否相同或相似?
  • 看起来它们是 0b 似乎可以制作文件但没有进行任何上传

标签: python image upload ftp ftplib


【解决方案1】:

我不完全确定出了什么问题,但这最终对我有用

import ftplib
import traceback

n="name of upload"
ftp = ftplib.FTP()
ftp.connect("website.com", "21")
print ftp.getwelcome()
try:
    try:
    ftp.login("user", "password")
    ftp.cwd("public_html/test")
    f = open("test.JPG", "rb")
    name= str(n)+".jpg"
    ftp.storbinary('STOR ' + name, f)
    f.close()
finally:
    ftp.quit()
except:
    traceback.print_exc()

希望这可以帮助其他处于同样困境的人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2021-02-14
    • 2023-03-14
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    相关资源
    最近更新 更多