From: Osuka Adartse Date: 2005-01-07T02:30:51+09:00 Subject: Re: Image decompression, eruby Belorion wrote: >>Shouldn't that be cgi.header("type"=>"image/jpg") ? > > > Yes, it should, and it is in my code ;) > > >>Just a guess: check for blanks/new lines around the <%= data %>. > > > Nope, can't find any blank lines.... > > >>The other possibility is to add a header "Content-Transfer-Encoding: base64" >>and send the base64 encoded photo directly. > > > Good idea. Gave it a shot, but couldn't get it to work. Same errors. > > >>I recommend storing the binary data in BLOB's or whatever that data type is >>called with mysql. Then you can insert data by doing >> insert into mytable (blobcol) values (?) >> >>The question mark is a placeholder for the data. You can bind your binary >>data to it by calling DBI#execute as follows: >> dbh.execute( stmt, data ) >> > > > I hadn't seen that before. I gave it a shot. Seems to work fine for > getting the browser to display an image. I take that back, dbh.do( > stmt, data ) works fine, I get an error with dbh.execute. However, > the image is coming back with glaring errors. After further > investigation it appears that the raw data in the MySQL table has > issues, though I can't tell if it's from the way the data is inserted, > or if is corrupted from the actual upload. > > So my first question regarding displaying the image via eruby in my > browser has been answered (thanks!), but maybe someone has an idea as > to why my data is being corrupted (the image comes back with a ton of > artifacts, especially with strange bars of color). > > The way I am uploading my file is derived from a script I found at > http://www.zytrax.com/tech/lang/ruby/#upload. > > Specifically, my HTML for uploading looks like: > >
action='attach.rhtml' method='post'> > > > > > > And my upload specific ruby in attach.rhtml looks like: > > fromfile = cgi.params['myfile'].first > fromfile.binmode # Tried both with and without this line > dbh.do( "INSERT INTO attachments VALUES( ? )", [ index, > fromfile.original_filename, fromfile.read] ) > > So, somewhere in that process the data gets corrupted, and the image > comes out half garbled. Any ideas? Plain text files, which are > handled using the exact same script, come out just fine. let me guess! The image starts OK but then horizontal lines start to appear with degenerating colors? if so the data in DB is probably OK as long as you use MySql::quote(img_data) at insert time. Ok try this, select the image data and write it to disk,just an idea since I had once a similar problem. res=@my.query("select pik from test where idpiks=1;") res.each{|r| img=r[0]} #well r[0] it's the actual image. File.open("e:/temp/test.jpg","wb").write(img) now compare it, if it's OK then no upload problems or in db but at "browser display". If not make sure the #quote method is being used. how I displayed the image imager.rb ... res=@my.query("select pik from test where idpiks=#{cgi.params['CID']};") res.each{|r| img=r[0]} puts "Content-Type: image/jpeg" puts "Content-Length: #{img.size}" puts "Content-Title:MyTest.jpg" puts $stdout.binmode << img #this is the culprit in windows, notice 'wb' mode in File#Write so #binmode, it drove me nuts back then, the answer was obvious... } hope it helps Adartse