-
Mar 7th, 2013, 02:33 PM
#1
Thread Starter
Member
Initiating HTTPWebRequest from jQuery
I need help, and a general understanding of how to use the HTTPWebRequest POST method. I need to pass a file from jQuery, Uploadify, on to a class file using HTTPWebRequest to issue multiple file uploads using the POST method. I don't understand how to pass the filename, which will need to be changed at some point during the upload, to my vb class file/HTTPWebRequest function. Also, I'm not sure my method is properly set up for uploading multiple files. There are so many examples on the web, I'm not certain if the method I'm attempting to use is prime. I'd appreciate any help I can get at this point as I am a little lost. My code follows jquery and vb:
Code:
$(function() {
$('#file_upload').uploadifive({
// Some options
'folder' : '/uploads/',
'buttonText': 'Select File(s)',
'method' : 'post',
'auto' : false,
'dnd' : false,
'queueID' : 'queue',
'uploadScript' : '/scripts/uploadifive.vb',
'cancelImg' : '/scripts/uploadifive-cancel.png',
'fileExt' : '*.txt;*.pdf;',
'fileDesc' : '*.txt and *.pdf',
'multi' : true,
'onUpload' : function(file, data, filesToUpload){
alert('The file ' + file.name + ' is being uploaded.');
var newFilePrefix = null;
newFile = selectedVal + '_' + file.name;
this.settings.formData = {'newFile': newFile }
},
'onUploadComplete' : function(event, queueID, file, response, data) {
$('#uploadhidden').val(file.name);
}
});
});
Code:
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
Public Shared Sub HttpUploadFile(url As String, file As String, paramName As String, contentType As String, nvc As NameValueCollection)
' Declare variable to access listboxes on main form
Dim lb As props = New props
' Declare listbox
Dim errLog As ListBox = lb.getLogControl("ErrorsLstbx")
Dim SummLog As ListBox = lb.getLogControl("SummLstbx")
Dim TransportLog As ListBox = lb.getLogControl("TransportMesslstbx")
' Declare sb to access stringbuilder property for logging
Dim sb As props = New props
' Add text line to stringbuilder then add to the listbox control
TransportLog.Text = sb.prop_sb_Transport(String.Format("Uploading {0} to {1}", file, url))
Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
Dim boundarybytes As Byte() = System.Text.Encoding.ASCII.GetBytes(vbCr & vbLf & "--" & boundary & vbCr & vbLf)
Dim wr As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
wr.ContentType = "multipart/form-data; boundary=" & boundary
wr.Method = "POST"
wr.KeepAlive = True
wr.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim rs As Stream = wr.GetRequestStream()
Dim formdataTemplate As String = "Content-Disposition: form-data; name=""{0}""" & vbCr & vbLf & vbCr & vbLf & "{1}"
For Each key As String In nvc.Keys
rs.Write(boundarybytes, 0, boundarybytes.Length)
Dim formitem As String = String.Format(formdataTemplate, key, nvc(key))
Dim formitembytes As Byte() = System.Text.Encoding.UTF8.GetBytes(formitem)
rs.Write(formitembytes, 0, formitembytes.Length)
Next
rs.Write(boundarybytes, 0, boundarybytes.Length)
Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""" & vbCr & vbLf & "Content-Type: {2}" & vbCr & vbLf & vbCr & vbLf
Dim header As String = String.Format(headerTemplate, paramName, file, contentType)
Dim headerbytes As Byte() = System.Text.Encoding.UTF8.GetBytes(header)
rs.Write(headerbytes, 0, headerbytes.Length)
Dim fileStream As New FileStream(file, FileMode.Open, FileAccess.Read)
Dim buffer As Byte() = New Byte(4095) {}
Dim bytesRead As Integer = 0
While (InlineAssignHelper(bytesRead, fileStream.Read(buffer, 0, buffer.Length))) <> 0
rs.Write(buffer, 0, bytesRead)
End While
fileStream.Close()
Dim trailer As Byte() = System.Text.Encoding.ASCII.GetBytes(vbCr & vbLf & "--" & boundary & "--" & vbCr & vbLf)
rs.Write(trailer, 0, trailer.Length)
rs.Close()
Dim wresp As WebResponse = Nothing
Try
wresp = wr.GetResponse()
Dim stream2 As Stream = wresp.GetResponseStream()
Dim reader2 As New StreamReader(stream2)
' Add text line to stringbuilder then add to the listbox control
TransportLog.Text = sb.prop_sb_Transport(String.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()))
Catch ex As Exception
' Add text line to stringbuilder then add to the listbox control
SummLog.Text = sb.prop_sb_Error("Error uploading file" & ex.ToString)
If wresp IsNot Nothing Then
wresp.Close()
wresp = Nothing
End If
Finally
wr = Nothing
End Try
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|