In the continuing series of articles that I have been writing in support of “Issues when trying to use multipart-form data with Jquery AJAX forms”, I wanted to address the challenges of using online code samples. I am a strong proponent of not typing code if I don’t have to. There are a lot of great sites out there that provide code snippets that can be used over and over again. Sites like this one or this one can be tremendous asset to any developer.
Inevitably you will come across a piece of code that you really want to use but it’s in a different language. Specifically I am talking about VB.net and C+ code. Most of us know that although these languages are similar, sometimes there are little (and large) nuances that can cause some pain. You could print out the C+ code or the VB.net code and manually convert it or you try several online conversion tools. If a simple- copy, paste and process click can save me time, than why not? At the end of the day you still have to understand the code because as good as they all might, there is always little changes that must be resolved manually. Take for example this block of code that I used for this project.
I could retype it into VB but why should I do this when I can convert it online. Each one has a little different type of conversion process and I have used all three at some point.
In the end you will have to determine which one translated better and then make your changes from there-to get this.
Public Function PostValue() As Task(Of HttpResponseMessage)
‘ Check if the request contains multipart/form-data.
If Not Request.Content.IsMimeMultipartContent() Then
Throw New HttpResponseException(HttpStatusCode.UnsupportedMediaType)
Dim provider = New InMemoryMultipartFormDataStreamProvider()
Dim test As System.IO.Stream
Dim task1 = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(Of HttpResponseMessage) _
‘access form data
Dim formdata As NameValueCollection = provider.FormData
Dim files As IList(Of HttpContent) = provider.Files
‘Example: reading a file’s stream like below
Dim file1 As HttpContent = files(0)
Dim file1Stream As Stream = file1.ReadAsStreamAsync().Result
Catch ex As Exception
Dim tr As String = ex.Message
It general these conversion tools are good at giving you a base start but in the end you still have to take the time to understand what you are doing.
Looking for quality web hosting? Look no further than Arvixe Web Hosting!