Problems uploading images

5 views
Skip to first unread message

dga

unread,
Jan 12, 2009, 12:49:27 PM1/12/09
to Big Five Safari iPhone Apps
Hi

I am trying to use the BigFive API to send an image to an ASP.NET page
via the "getFromPhotoLibrary" api method; I seem to be unable to
properly reconstruct the image data.

This is the code that runs when the page loads (I have confirmed that
the page is loaded and that the "data" parameter exists):

If Request.Params("data") IsNot Nothing Then
Try
' write data into testimage.dat file
File.WriteAllText("c:\temp\testimage.dat",
Request.Params("data"))

' get data as a binary array'
Dim encoding As New System.Text.ASCIIEncoding()
Dim ImageData As Byte() = encoding.GetBytes
(Request.Form("data"))

' create the actual jpg file
Dim fs As New FileStream("c:\temp\test.jpg",
FileMode.Create)
fs.Write(ImageData, 0, ImageData.Length)
fs.Close()

Catch ex As Exception
Dim err As String = ex.Message + vbCrLf +
ex.StackTrace
File.WriteAllText("c:\temp\error.txt", err)
End Try

End If

The code writes two files. The first (called "testimage.dat") simply
contains the data returned in the "data" parameter. The second
(called "test.jpg") should be the image itself, but it is reported as
corrupt and cannot be displayed. How is this image data encoded?

Also, I have noticed that the image upload to the demo site no longer
works (at least from my phone). Is this potentially related to this
problem?

Thanks

Dirk Holtwick

unread,
Jan 12, 2009, 1:11:24 PM1/12/09
to big5...@googlegroups.com
Hi,

it seems that I messed up a bit the online demos while writing the new
API for Big Five. I am fixing these issues right now.

Now to your questions: The coding of the image in the POST variable
should be of the format JPEG (quality 100%). I do not know ASP very
well, but I see something like "ASCIIEncoding" and so on. Wouldn't it be
possible to do something like:

Dim fs As New FileStream("c:\temp\test.jpg", FileMode.Create)
fs.WriteAllBinary(Request.Params("data"))
fs.Close()

Or try to open the file you first create:

File.WriteAllText("c:\temp\testimage.jpg", Request.Params("data"))

Are the created files bigger than 0 Bytes? Maybe ASP needs explicit call
for POST parameters?

The demo I have written uses Python and Pyxer on the Google App Engine
and therefore maybe is not very self expression, but anyways here is the
code:

-----------------8<---------------[cut here]
from google.appengine.ext import db

class DemoImage(db.Model):
uid = db.StringProperty()
data = db.BlobProperty()
added = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)

@controller
def upload():
if req.body:
uid = req.params.get("uid", "")
data = req.POST.get("data", "---")
img = DemoImage(data=data, uid=uid)
img.put()
return uid
return "ERROR"

@expose
def show(uid):
if not uid:
res = db.GqlQuery("SELECT * FROM DemoImage ORDER BY added DESC")
else:
res = db.GqlQuery("SELECT * FROM DemoImage WHERE uid=:uid ORDER
BY added DESC", uid=uid)
row = res.get()
if row:
response.content_type = "image/jpeg"
return str(row.data)
return "ERROR"
-----------------8<---------------[cut here]

Maybe someone else has working version for this?

Cheers
Dirk

dga schrieb:

dga

unread,
Jan 12, 2009, 4:30:56 PM1/12/09
to Big Five Safari iPhone Apps
Thanks.

The code sample confirmed my suspicion that the problem was asp.net
converting the form value to a string. After some effort, I came up
with this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' get the id to associate with the image
If Request.Params("uid") IsNot Nothing Then
File.WriteAllText("c:\temp\test.dat", Request.Params
("uid").ToString())
End If

' if a data form value exists, attempt to read the image
' we cannot just use Request.Form("data") because the data in
this variable has
' been converted to a string
If Request.Form("data") IsNot Nothing Then

Try

' get the request input stream
Dim stream As Stream = Request.InputStream()
Dim x1 As Integer, x2 As Integer
Dim ImageBytes As New List(Of Byte)

' read inital byte
x2 = stream.ReadByte()

' loop until all bytes are read
Do
' get next byte
x1 = stream.ReadByte()
'and exit if none was found
If x1 = -1 Then Exit Do

' are we are still looking for the jpeg header?
If ImageBytes.Count = 0 Then
' yes - see if we have it
If x2 = 255 And x1 = 216 Then
ImageBytes.Add(Convert.ToByte(x2))
ImageBytes.Add(Convert.ToByte(x1))
Else
x2 = x1
End If
Else
' we have found the header, just add bytes
ImageBytes.Add(Convert.ToByte(x1))
End If
Loop

' remove last 20 items from byte list
If ImageBytes.Count > 20 Then ImageBytes.RemoveRange
(ImageBytes.Count - 21, 20)

' create the image
Dim img As Image = Image.FromStream(New MemoryStream
(ImageBytes.ToArray()))
img.Save("c:\temp\test.jpg")

Catch ex As Exception
Dim err As String = ex.Message + vbCrLf +
ex.StackTrace
File.WriteAllText("c:\temp\error.txt", err)
End Try

End If

End Sub

This works, but I am not convinced that there isn't a better way

Cheers

David Ball

Dirk Holtwick

unread,
Jan 13, 2009, 9:27:04 AM1/13/09
to big5...@googlegroups.com
Great. I'm happy that is it working with ASP. Has anyone else working
examples in other environments like Python? Would you mind if I add your
example to the projects Wiki?

http://code.google.com/p/big5/wiki/ImageUpload

Dirk

dga schrieb:
Reply all
Reply to author
Forward
0 new messages