![](/img/trans.png)
[英]Python - Getting file size of file object from file upload in Flask
[英].rpm file size increased (getting corrupted) during upload in python
我正在從Django UI上傳一個.rpm文件。 我可以在所需位置成功上傳文件。
問題:上傳后文件大小增加,由於這個原因,我在嘗試提取該.rpm文件時遇到錯誤-“從包中讀取標題錯誤”
以下是我用來上傳文件的功能:
// RPM_DIR = some DIR path where I am saveing the file"
def save_temporarily(file, name):
with open(os.path.join(RPM_DIR,str(name)),"wb+") as destination:
for chunk in file.chunks():
destination.write(chunk)
destination.closed
f.seek(0)
return os.path.join(RPM_DIR,str(name))
ls -ltr
輸出
-rw-r--r-- 1個根目錄3748319 2月20日new_file.rpm(用於新上傳的文件)
-rw-r--r-- 1個根目錄3735417 2月20日xyz.rpm(對於原始文件)
尺寸增加了...
請提出如何解決此問題的建議...特別是,如果可能,我正在尋找以下解決方案
編輯
我也嘗試將write函數更改為
output_file_path = "/u001/Test/"+ file.name
result_file = open(output_file_path,"wb")
while True:
file_content = file.read(1024) ''' or simply file.read() '''
if not file_content:
break
result_file.write(file_content)
result_file.write(file_content)
result_file.close()
我得到的輸出相同,沒有變化...保存.rpm文件后,我實際上正在運行以下命令( 有關詳細信息,請參見 ):
rpm2cpio '+str(patch_path)+' | cpio -idm
並得到以下錯誤:
<open file 'rpm2cpio /u001/Test/php-5.1.4-1.esp1.x86_64.rpm | cpio -idm ', mode 'r' at 0x7f6334239030>
error: rpm2cpio: headerRead failed: region trailer: BAD, tag 491913216 type 508690432 offset -525467648 count 542113792
error reading header from package
cpio: premature end of archive
PS:這可能有助於更多地了解正在發生的事情
謝謝,
您是否對兩個文件做了二進制差異(unix:cmp),以查看new_file.rpm損壞的地方? 我想知道您的問題是否不是多余的字節而是損壞。
Python文件對象的write()方法采用一個字符串,對該字符串的處理可能會根據您的字符編碼而有所不同。 RPM文件是二進制的。 從您的代碼示例中尚不清楚對象文件.chunks()返回什么類型。
可能您需要執行此處建議的操作: Python如何寫入二進制文件?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.