Home
> Uncategorized > Uploading a file via a web service
Uploading a file via a web service
When you think uploading files with a web service, you may immediately think about using DIME and WSE 2.0 etc. However, I believe there is an easier way – depending on your application, I presume, but this approach may be useful.
I wanted to be able to allow people use standard File upload from a webpage
<input type="file"> – and then have the output of this passed to a webservice. – Also, I wanted to do it generically, so that if an application were to consume the webservice, the developer would not have to figure out how to cast a stream to a HttpPostedFile class.
Therefore, I used a very simple technique, use a byte array as a parameter to the webMethod.
– I then wanted to save the byte array as a file, with a random name, in the same path as the webservice –
string
strPath = HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString(); string strFileName = (Guid.NewGuid()).ToString()+".wav";FileStream fsWave =
new FileStream(strPath + strFileName,FileMode.Create);fsWave.Write(WaveData,0,WaveData.Length);
fsWave.Close();
Where WaveData was my Byte array.
Then when consuming this service, from an ASP.NET page, I used this code
HttpPostedFile hpfWave =
this.AudioFile.PostedFile; int intFileLen = hpfWave.ContentLength; byte[] bWaveData = new byte[intFileLen];hpfWave.InputStream.Read(bWaveData, 0, intFileLen);
strMessage = vmWebService.UploadAudio(bWaveData);
Where AudioFile is my Input type=file on the web page, and vmWebService was my webservice.
If you’re interested in knowing where this is leading, check out www.sms-txt.co.uk in a few days!
Categories: Uncategorized
Comments (0)
Trackbacks (0)
Leave a comment
Trackback