sorry again stewsterl you are not understanding me.
I have a site where people can upload files to. I save them on the server and when the user logs in i give them access to this file by going to my database and getting the file location like "c:\username\photos\cat.jpg"
i have to make this function call work
SendFileToPicknik("C:\testdocs\Creek.jpg")
Code Listed Below
Imports System.Net
Imports System.IO
Partial Class picnik : Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SendFileToPicknik("C:\testdocs\Creek.jpg")
End Sub
Sub SendFileToPicknik(ByVal sFullPath As String)
Dim sPicnikURL As String = "
http://www.picnik.com/service/"
Dim sApiKey As String = "hidden"
Dim sFileName As String = Path.GetFileName(sFullPath)
Dim sFileNameForSaving As String = sFileName
Dim fStream As FileStream
Dim sCallingURL As String = Request.Url.AbsoluteUri.ToString
Try
Dim webrequest As HttpWebRequest = HttpWebRequest.Create(sPicnikURL)
'Create a boundary item for use in the header
Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
webrequest.Method = "POST"
webrequest.KeepAlive = True
webrequest.Headers.Add("Keep-Alive", "300")
webrequest.ContentType = "multipart/form-data; boundary=" & boundary
webrequest.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
webrequest.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
webrequest.Headers.Add("Accept-Encoding", "gzip, deflate")
webrequest.Headers.Add("Accept-Language", "en-us,en;q=0.5")
webrequest.Referer = sCallingURL
webrequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"
webrequest.Expect = ""
' Build up the post message header
Dim sb As New StringBuilder
'File Details
sb.Append("--")
sb.Append(boundary)
sb.Append(vbCrLf)
sb.Append("Content-Disposition: form-data; name=""")
sb.Append("image_file")
sb.Append("""; filename=""")
sb.Append(sFileName) 'Replace with the name of your file
sb.Append("""")
sb.Append(vbCrLf)
sb.Append("Content-Type: ")
sb.Append("image/jpeg") 'This can be left blank if you are unsure what type to use
sb.Append(vbCrLf)
sb.Append(vbCrLf)
'Store this seperately to the rest of the header, it must appear before the file contents (see below)
Dim fileHeader As String = sb.ToString()
Dim fileHeaderBytes As Byte() = Encoding.UTF8.GetBytes(fileHeader)
'Create the rest of the post header
sb = New StringBuilder()
'FileName attribute
sb.Append(vbCrLf)
sb.Append("--")
sb.Append(boundary)
sb.Append(vbCrLf)
sb.Append("Content-Disposition: form-data; name=""")
sb.Append("_import")
sb.Append("""")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("image_file")
'_returntype
sb.Append(vbCrLf)
sb.Append("--")
sb.Append(boundary)
sb.Append(vbCrLf)
sb.Append("Content-Disposition: form-data; name=""")
sb.Append("_returntype")
sb.Append("""")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("text")
'_export_field
sb.Append(vbCrLf)
sb.Append("--")
sb.Append(boundary)
sb.Append(vbCrLf)
sb.Append("Content-Disposition: form-data; name=""")
sb.Append("_export_field")
sb.Append("""")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("file")
'_apikey
sb.Append(vbCrLf)
sb.Append("--")
sb.Append(boundary)
sb.Append(vbCrLf)
sb.Append("Content-Disposition: form-data; name=""")
sb.Append("_apikey")
sb.Append("""")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("hidden")
'close the header
sb.Append(vbCrLf)
sb.Append("--")
sb.Append(boundary)
sb.Append("--")
'Store the remaining header as a string and convert to a byte array
Dim postHeader As String = sb.ToString()
Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader)
'Open the file
fStream = New FileStream(sFullPath, FileMode.Open, FileAccess.Read, FileShare.None)
Dim bytesRead As Integer = 0
Dim buffer(fStream.Length) As Byte
bytesRead = fStream.Read(buffer, 0, buffer.Length)
'Close the file stream
fStream.Close()
'Get the length of the header.
'This is NOT the length of the strings, but the length of their corresponding byte array
Dim length As Long = fileHeaderBytes.Length + buffer.Length + postHeaderBytes.Length
'Set content length
webrequest.ContentLength = length
'Open the request stream
Dim requestStream As Stream = webrequest.GetRequestStream()
'WRITE INFO INTO RESPONSE STREAM
'write FileHeader to stream
requestStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length)
'write File to stream
requestStream.Write(buffer, 0, buffer.Length)
'write remaning header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
'get response
Dim aResponse As WebResponse = webrequest.GetResponse()
Dim url As String = aResponse.ResponseUri.AbsoluteUri.ToString
Response.Redirect(url)
Catch ex As Exception
'Handle the error accordingly
Throw ex
Finally
If Not IsNothing(fStream) Then
fStream.Close()
End If
End Try
End Sub
End Class