Image upload to one site, save data in another

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I have a form that captures information about a person and provides a way to
upload an image. The image is saved to a hosting service and the person's
information (including the image and thumbnail's url) are stored in the
application's repository.

One issue is that i need to post the image first, then parse the response to
find the two urls (image and thumbnail).

The second issue is that i would like to post the image directly to the
hosting service, rather than having to route it thru my application.

My first thought is to upload the image with an ajax call to the image
server, then parse the response, then do a normal postback.

Any thoughts on this topic would be appreciated.

Thanks in advance,

Craig Buchanan
 
Is the upload application unable to access the database for the "other"
application? If not, two applications can share the same data repository. If
so, I am not completely understanding your application(s) architecture.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*************************************************
| Think outside the box! |
*************************************************
 
I can't access the persistence (image and metadata) on the other site
(imagehost.com). after an image is posted, it returns a form w/ various
image information, including the urls for the image and its thumbnail. the
company doesn't have an xml web service per se, so this is a hack.

essentially, i need to store my application's data at my site, but host the
images at another (free) site. i need the urls from the image site to be
saved in my application's data store, obviously.

thanks,

Craig
 
I decided to have my server act as the proxy and repost the image to the
other server. seems to work, but i'm not getting the expected HTML
returned--i think i need to add additional Form variables to the POST. Any
thoughts on how to do this would be appreciated.

Here's the code thus far:

Try
'post picture to image server

Dim WebRequest As HttpWebRequest =
CType(HttpWebRequest.Create("http://www.imagehosting.com/host.php"),
HttpWebRequest)

With WebRequest

..Method = "POST"

..ContentType = "multipart/form-data"

..ContentLength = Me.FileUpload1.FileBytes.Length

..AllowWriteStreamBuffering = True

'TODO POST other form variables

'not sure how to do this yet

'POST the contents of the file upload control

Using requestStream As System.IO.Stream = .GetRequestStream()

requestStream.Write(Me.FileUpload1.FileBytes, 0,
Me.FileUpload1.FileBytes.Length)

End Using

End With

Dim WebResponse As HttpWebResponse = CType(WebRequest.GetResponse,
HttpWebResponse)

With WebResponse

If .StatusCode = HttpStatusCode.OK Then

Using responseStream As System.IO.Stream = .GetResponseStream

Dim html As String = String.Empty

Using sr As New System.IO.StreamReader(responseStream, Encoding.UTF8, True)

html = sr.ReadToEnd

End Using

End Using

End If

..Close()

End With

Catch ex As Exception

Throw

End Try
 
After something thinking, I figured that I needed to post a multi-part form.
I refactored my code and added the missing logic. Here is the result:

Public Shared Function RemoteFileUpload(ByVal url As String, ByRef file As
Web.HttpPostedFile, ByVal elements As NameValueCollection) As String

Dim boundary As String = "----------------------------" &
DateTime.Now.Ticks.ToString("x")

Dim boundarybytes As Byte() = System.Text.Encoding.ASCII.GetBytes(vbCr &
vbLf & "--" & boundary & vbCr & vbLf)

Dim memStream As Stream = New System.IO.MemoryStream()

Try

'

'add form elements to stream

'

'fields are a sequence of (name, value) elements for regular form fields.

Dim formdataTemplate As String = vbCr & vbLf & "--" & boundary & vbCr & vbLf
& "Content-Disposition: form-data; name=""{0}"";" & vbCr & vbLf & vbCr &
vbLf & "{1}"

For Each key As String In elements.Keys

Dim formitem As String = String.Format(formdataTemplate, key, elements(key))

Dim formitembytes As Byte() = System.Text.Encoding.UTF8.GetBytes(formitem)

memStream.Write(formitembytes, 0, formitembytes.Length)

Next

'delimiter

memStream.Write(boundarybytes, 0, boundarybytes.Length)

'

'add file's metadata to stream

'

Dim header As String, headerData As Byte()

'file's content disposition

Const CONTENT_DISPOSITION = "Content-Disposition: form-data; name=""{0}"";
filename=""{1}""" & vbCr & vbLf

header = String.Format(CONTENT_DISPOSITION, "fileupload", file.FileName)

headerData = System.Text.Encoding.UTF8.GetBytes(header)

memStream.Write(headerData, 0, headerData.Length)

'file's content type

'extra CR LF as it is the last header

Const CONTENT_TYPE As String = " Content-Type: {0}" & vbCr & vbLf & vbCr &
vbLf

header = String.Format(CONTENT_TYPE, file.ContentType)

headerData = System.Text.Encoding.UTF8.GetBytes(header)

memStream.Write(headerData, 0, headerData.Length)

'

'add file to stream

'

'convert stream to byte()

Dim buffer As Byte() = New Byte(file.InputStream.Length - 1) {}

file.InputStream.Position = 0

file.InputStream.Read(buffer, 0, buffer.Length)

file.InputStream.Close()

'write file's bytes

memStream.Write(buffer, 0, buffer.Length)

'delimiter

memStream.Write(boundarybytes, 0, boundarybytes.Length)

'convert stream to byte()

ReDim buffer(memStream.Length - 1)

memStream.Position = 0

memStream.Read(buffer, 0, buffer.Length)

memStream.Close()

'open connection to server

Dim WebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url),
HttpWebRequest)

With WebRequest

..ContentType = "multipart/form-data; boundary=" & boundary

..Method = "POST"

..KeepAlive = True

..Credentials = System.Net.CredentialCache.DefaultCredentials

..ContentLength = buffer.Length

End With

'POST form and file's bytes

Using requestStream As System.IO.Stream = WebRequest.GetRequestStream()

requestStream.Write(buffer, 0, buffer.Length)

End Using

Dim html As String = String.Empty

'get response from posting

Using WebResponse As HttpWebResponse = CType(WebRequest.GetResponse,
HttpWebResponse)

With WebResponse

Select Case .StatusCode

Case HttpStatusCode.OK

Using responseStream As System.IO.Stream = .GetResponseStream

Using sr As New System.IO.StreamReader(responseStream, Encoding.UTF8, True)

html = sr.ReadToEnd

End Using

End Using

Case Else

'TODO: error handling

End Select

End With

End Using

Return html

Catch ex As Exception

Throw

Finally

memStream.Dispose()

End Try

End Function
 
Back
Top