簡體   English   中英

無法使用Ruby和Windows 7上傳PDF(使用Linux沒有問題)

[英]Unable to upload PDF using Ruby and Windows 7 (no issues using Linux)

我正在嘗試使用Ruby上傳PDF。 我的代碼在Linux機器上可以正常工作,但在我現在嘗試使用的Windows 7機器上卻無法正常工作。 我在Windows中使用Ruby 2.2.0,而在Linux中使用2.3.0。

如果我在IRB中輸入以下內容,則在Windows和Linux上我的結果會大不相同:

require 'stringio'

fname = 'C:\Sites\Programming\watir-webdriver\websign_tests\eStore.pdf' 
# vs on Linux fname = '/home/me/Programming/watir-webdriver/websign_tests/eStore.pdf'
# forward slashes and backslashes give the same end result on Windows

File.readable?(fname) # => true in Windows and Linux

f_obj = File.open(fname)

f_data = StringIO.new(f_obj.read)
f_data.size # => 416 in Windows vs 186550 on Linux

#There is much less output for the following statement in Windows
#than there is in Linux
File.foreach(f_obj) {|x| print "GOT ", x }

GOT %PDF-1.4
GOT %JetForm PDF Support Version 2.3.000
GOT %EncodingObject=0
GOT %√ⁿ²■
GOT 1 0 obj
GOT <<
GOT /Type /Catalog
GOT /Pages 3 0 R
GOT /Outlines 4 0 R
GOT >>
GOT endobj
GOT
GOT 5 0 obj
GOT <<
GOT /Length 0000038826 /Filter/FlateDecode/Length1 0000057578 >>
GOT stream
GOT x┌î|        |SU÷ =≈¡y/╦╦₧4]ƪI)à╢P
GOT ¶½}╩Zö]û"Ñe)P♦);╚«`í T
GOT éêRE♦§ñÇB┴♣T\G♣ù↓up♦↔╘Y∞ê♫:ÄÆ⌠⌂εK[`µ≈√}■I≤÷Σ¥{╓∩9≈╝↕ äÿ╔J┬◄}╚ê▄«╒τ∩|
GOT Å╝ïƒQôfN¿₧8ΩïrB â►±²I♂µ♣∩z╝d.!╥eB°ëS¬º╬¼ε╡⌠Sⁿü1╕⌂rΩî┼Sε¢₧qà►╗ÅÉ¥╒╙*'L~íΣ⌐┴ä∟
Xï┐╫}=> nil

我已經確認Windows機器上的PDF文件大小為186,550字節,並且可以在Adobe Reader中正常打開。

我不確定為什么無法使用Ruby和Windows 7完成此任務。我是在做錯什么,還是這是Ruby / Windows錯誤?

如果是錯誤,是否還有另一種方法可以完成此任務?

PDF是一個二進制文件,因此您需要告訴File.open使用二進制模式。

這主要是Windows特有的,否則它將把文件當作文本文件對待,而在Linux上則不需要二進制修飾符(它會被忽略)。

因此,這就是您需要的:

f_obj = File.open(fname, "rb")

您可以在此處閱讀有關二進制標志的更多信息:
https://softwareengineering.stackexchange.com/questions/238976/what-does-it-mean-that-file-io-binary-flag-is-only-relevant-on-windows

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM