File Upload in Silverlight Using WCF
In previous Articles we have discused that how we can upload a dile using .asmx web service and isolated storage space. Here in continution to the topic I am going to discuss how we can use WCF for the same purpose.
Steps :
- Create the a silverlight project with the “FileUploadWCF” name.
- Make these important changes in the properties of FileUploadWCF.Web project
- Right click on FileUploadWCF.Web.
- Select Properties
-
Under Web tab Select Specific port and give the port number I am using port number 1366
Note: we are using specific port so that every time we need not to run the
web service to use it and it wont change its address. Neither we ned it to host on IIS if you don’t want to use specifc port then either host your
web service on IIS or run the webservice every time and update the service refrence each time
before running the application. - Save The Properties Page and close it.
- Now Add a silverlight Comaptible WCf to the Project and name it as uploadService
- Include the name space “System.IO”
<font COLOR="#0000ff">using</font> System.IO;
- Add a operation Contract as :
[<font COLOR="#2b91af">OperationContract</font>]<br />
<font COLOR="#0000ff">public</font> <font COLOR="#2b91af">List</font><<font COLOR="#0000ff">string</font>>
UploadFiles(<font COLOR="#0000ff">string</font> strFileName,
<font COLOR="#0000ff">byte</font>[] byFile, <font COLOR="#0000ff">string</font>
strFileNo)<br />
{<br />
<font COLOR="#2b91af"> List</font><<font COLOR="#0000ff">string</font>>
result = <font COLOR="#0000ff">new</font> <font COLOR="#2b91af">List</font><<font COLOR="#0000ff">string</font>>();<br />
<font COLOR="#0000ff"> try<br />
</font>
{<br />
<font COLOR="#008000"> //if file length is greater then 0 <br />
</font>
<font COLOR="#0000ff">if</font> (byFile.Length > 0)<br />
{<br />
<font COLOR="#008000"> //set the location in a string variable where to save
the file<br />
</font>
<font COLOR="#0000ff">string</font> strFilePath = <font COLOR="#a31515">
"D:\\meet\\"</font> + strFileName;<br />
<font COLOR="#008000"> //create a filestream<br />
</font>
System.IO.<font COLOR="#2b91af">FileStream</font> fs = <font COLOR="#0000ff">
new</font> <font COLOR="#2b91af">FileStream</font>(strFilePath,
<font COLOR="#2b91af">FileMode</font>.Create, <font COLOR="#2b91af">FileAccess</font>.Write);<br />
<font COLOR="#008000"> //write the file at the specified location<br />
</font>
fs.Write(byFile, 0, byFile.Length);<br />
<font COLOR="#008000"> //close file stream<br />
</font>
fs.Close();<br />
result.Add(strFileNo);<br />
<font COLOR="#008000"> //returm file number<br />
</font>
<font COLOR="#0000ff">return</font> result;<br />
}<br />
<font COLOR="#0000ff"> else<br />
</font>
{<br />
result.Add(<font COLOR="#a31515">""</font>); <br />
<font COLOR="#0000ff"> return</font> result;<br />
}<br />
}<br />
<font COLOR="#0000ff"> catch<br />
</font>
{<br />
result.Add(<font COLOR="#a31515">""</font>);<br />
<font COLOR="#0000ff"> return</font> result;<br />
}<br />
}
- Create A class "FilesClass" in FileUploadWCF project
<font COLOR="#0000ff">public</font> <font COLOR="#0000ff">
class</font> <font COLOR="#2b91af">FilesClass<br />
</font>
{<br />
<font COLOR="#0000ff"> public</font> FilesClass()<br />
{<br />
<font COLOR="#008000"> //do nothing<br />
</font>
}<br />
<font COLOR="#0000ff"> string</font> strStatus = <font COLOR="#a31515">""</font>;<br />
<font COLOR="#0000ff"> string</font> strNo = <font COLOR="#a31515">""</font>;<br />
FileInfo strFileName = <font COLOR="#0000ff">null</font>;<br />
<font COLOR="#0000ff"> public</font> <font COLOR="#0000ff">string</font>
PropNumber<br />
{<br />
<font COLOR="#0000ff"> get<br />
</font>
{<br />
<font COLOR="#0000ff"> return</font> strNo;<br />
}<br />
<font COLOR="#0000ff"> set<br />
</font>
{<br />
strNo = <font COLOR="#0000ff">value</font>;<br />
}<br />
}<br />
<font COLOR="#0000ff"> public</font> FileInfo PropFileName<br />
{<br />
<font COLOR="#0000ff"> get<br />
</font>{<br />
<font COLOR="#0000ff"> return</font> strFileName;<br />
}<br />
<font COLOR="#0000ff"> set<br />
</font>
{<br />
strFileName = <font COLOR="#0000ff">value</font>;<br />
}<br />
}<br />
<font COLOR="#0000ff"> public</font> <font COLOR="#0000ff">string</font>
PropStatus<br />
{<br />
<font COLOR="#0000ff"> get<br />
</font>
{<br />
<font COLOR="#0000ff"> return</font> strStatus;<br />
}<br />
<font COLOR="#0000ff"> set<br />
</font>
{<br />
strStatus = <font COLOR="#0000ff">value</font>;<br />
}<br />
}<br />
} -
Create Two Buttons in the Mainpage.xaml.
- To Chosse File
- To upload File At the specifed folder in the Web Service
The Code Will look like
<font COLOR="#a31515"><p></font><font COLOR="#0000ff"><</font><font COLOR="#a31515">Grid</font><font COLOR="#ff0000">
x</font><font COLOR="#0000ff">:</font><font COLOR="#ff0000">Name</font><font COLOR="#0000ff">="LayoutRoot"</font><font COLOR="#ff0000">
Background</font><font COLOR="#0000ff">="White"</font><font COLOR="#ff0000">
Width</font><font COLOR="#0000ff">="400"</font><font COLOR="#ff0000"> Height</font><font COLOR="#0000ff">="50"></p>
</font><font COLOR="#a31515">
<p></font><font COLOR="#0000ff"><</font><font COLOR="#a31515">Button</font><font COLOR="#ff0000">
Content</font><font COLOR="#0000ff">="Choose"</font><font COLOR="#ff0000">
Height</font><font COLOR="#0000ff">="20"</font><font COLOR="#ff0000"> Width</font><font COLOR="#0000ff">="150"</font><font COLOR="#ff0000">
Click</font><font COLOR="#0000ff">="Button_Click"</font><font COLOR="#ff0000">
Margin</font><font COLOR="#0000ff">="10,0,10,10"</font><font COLOR="#ff0000">
VerticalAlignment</font><font COLOR="#0000ff">="Bottom"</font><font COLOR="#ff0000">
d</font><font COLOR="#0000ff">:</font><font COLOR="#ff0000">LayoutOverrides</font><font COLOR="#0000ff">="Width,
Height"</font><font COLOR="#ff0000"> HorizontalAlignment</font><font COLOR="#0000ff">="Left"</font><font COLOR="#ff0000">
Background</font><font COLOR="#0000ff">="#FF2770AF"></</font><font COLOR="#a31515">Button</font><font COLOR="#0000ff">></p>
</font><font COLOR="#a31515">
<p></font><font COLOR="#0000ff"><</font><font COLOR="#a31515">Button</font><font COLOR="#ff0000">
Content</font><font COLOR="#0000ff">="Upload"</font><font COLOR="#ff0000">
Height</font><font COLOR="#0000ff">="20"</font><font COLOR="#ff0000"> Width</font><font COLOR="#0000ff">="150"</font><font COLOR="#ff0000">
Click</font><font COLOR="#0000ff">="Button_Click_1"</font><font COLOR="#ff0000">
Margin</font><font COLOR="#0000ff">="20,0,10,10"</font><font COLOR="#ff0000">
VerticalAlignment</font><font COLOR="#0000ff">="Bottom"</font><font COLOR="#ff0000">
d</font><font COLOR="#0000ff">:</font><font COLOR="#ff0000">LayoutOverrides</font><font COLOR="#0000ff">="Width,
Height"</font><font COLOR="#ff0000"> HorizontalAlignment</font><font COLOR="#0000ff">="Right"</font><font COLOR="#ff0000">
Background</font><font COLOR="#0000ff">="#FF2770AF"></</font><font COLOR="#a31515">Button</font><font COLOR="#0000ff">></p>
</font><font COLOR="#a31515">
<p></font><font COLOR="#0000ff"></</font><font COLOR="#a31515">Grid</font><font COLOR="#0000ff">></p>
</font>
- Add the service refernce to the silverlight project name it “uploadService”
- Navigating to the event for choose button
<font COLOR="#0000ff">private</font> <font COLOR="#0000ff">
void</font> Button_Click(<font COLOR="#0000ff">object</font> sender,
<font COLOR="#2b91af">RoutedEventArgs</font> e)<br />
{<br />
<font COLOR="#008000"> //create a oblect for file open dialog<br />
</font>
<font COLOR="#2b91af">OpenFileDialog</font> op = <font COLOR="#0000ff">new</font>
<font COLOR="#2b91af">OpenFileDialog</font>();<br />
<font COLOR="#008000"> //show the file browser dialog to select file<br />
</font>
op.ShowDialog();<br />
<font COLOR="#008000"> //if any file is selected<br />
</font>
<font COLOR="#0000ff">if</font> (op.File != <font COLOR="#0000ff">null</font>
&& op.File.Name != <font COLOR="#a31515">""</font>)<br />
{<br />
<font COLOR="#008000"> //check the file size. It shoud be less than 1MB <br />
</font>
<font COLOR="#0000ff">if</font> (op.File.Length < 1048576)<br />
{<br />
<font COLOR="#008000"> //create a new fill class object to maintain the file
content <br />
</font>
<font COLOR="#2b91af">FilesClass</font> obj = <font COLOR="#0000ff">new</font>
<font COLOR="#2b91af">FilesClass</font>();<br />
<font COLOR="#008000"> //assign file <br />
</font>
obj.PropFileName = op.File;<br /><font COLOR="#008000"> //assing file number<br />
</font>
obj.PropNumber = iFileCount.ToString();
<br />
<font COLOR="#008000"> //add the file object to files list collection <br />
</font>
fl.Add(obj);<br/>
iFileCount++;<br />
}<br />
<font COLOR="#008000"> //IF size is greater then 1 MB show the message<br />
</font>
<font COLOR="#0000ff">else<br />
</font>
{<br />
<font COLOR="#2b91af"> MessageBox</font>.Show(<font COLOR="#a31515">"Max file
size is 1 MB"</font>);<br />
}<br />
}<br />
<font COLOR="#008000"> //display message to the user if no file is selected<br />
</font>
<font COLOR="#0000ff">else<br />
</font>
{<br />
<font COLOR="#2b91af"> MessageBox</font>.Show(<font COLOR="#a31515">"Select
File"</font>);<br />
}<br />
} -
Now Lets code for the upload event Navigate to the event
<font COLOR="#0000ff">private</font>
<font COLOR="#0000ff">void</font> Button_Click_1(<font COLOR="#0000ff">object</font>
sender, <font COLOR="#2b91af">RoutedEventArgs</font> e)<br />
{<br />
<font COLOR="#0000ff"> if</font> (fl.Count > 0)<br />
{<br />
<font COLOR="#008000"> //loop through the files and upload the files by
passing it to web service.<br />
</font>
<font COLOR="#0000ff">for</font> (<font COLOR="#0000ff">int</font> count =
0; count < fl.Count; count++)<br />
{ <br />
uploadService.<font COLOR="#2b91af">uploadServiceClient</font> x =
<font COLOR="#0000ff">new</font> uploadService.<font COLOR="#2b91af">uploadServiceClient</font>();<br />
<font COLOR="#008000"> //read the file from file class<br />
</font>
<font COLOR="#2b91af">FilesClass</font> obj = (<font COLOR="#2b91af">FilesClass</font>)fl[count];<br />
System.IO.<font COLOR="#2b91af">FileStream</font> str =
obj.PropFileName.OpenRead();<br />
<font COLOR="#0000ff"> byte</font>[] by = <font COLOR="#0000ff">new</font>
<font COLOR="#0000ff">byte</font>[str.Length];<br />
str.Read(by, 0, by.Length);<br />
str.Close();<br />
<font COLOR="#008000"> //call the upload file methods async event, which will
upload the files asynchronously<br />
</font>
x.UploadFilesAsync(obj.PropFileName.Name, by, obj.PropNumber);<br />
<font COLOR="#008000"> //here is the event hadndler, whihc will be invoked
for every file upload<br />
</font>
x.UploadFilesCompleted += <font COLOR="#0000ff">new</font> <font COLOR="#2b91af">EventHandler</font><FileUploadWCF.uploadService.<font COLOR="#2b91af">UploadFilesCompletedEventArgs</font>>(UploadFileComplted);<br />
}<br />
}<br />
}<br />
<font COLOR="#0000ff">private</font> <font COLOR="#0000ff">void</font>
UploadFileComplted(<font COLOR="#0000ff">object</font> sender, uploadService.<font COLOR="#2b91af">UploadFilesCompletedEventArgs</font>
e)<br />
{<br />
<font COLOR="#0000ff"> if</font> (e.Result.ElementAt(0).ToString()!=<font COLOR="#a31515">""</font>)<br />
{<br />
<font COLOR="#2b91af"> MessageBox</font>.Show(<font COLOR="#a31515">"File
Uploaded"</font>);<br />
}<br />
}
The Code Is Attached you can Download the files and enjoy file handling
Thanks and Regards
Meetu Choudhary
MVP.
My Blog || My Web || My Forums
Popularity: 12%
Related posts:
- File Upload In silverlight using Web service (.asmx) In My Article File Handling in Silverlight I have discussed...
- File Handling in Silverlight ...
- Isolated Storage in the Silverlight Application Isolated Storage Isolated Storage in the Silverlight Application Definition: Isolated...
- Changing BackGround Color using JavaScript The following code shows how on button click event we...
- Sending Mails with attachments using Gmail Tweet This Post...
Related posts brought to you by Yet Another Related Posts Plugin.


(2 votes, average: 4.00 out of 5)







Leave your response!
You must be logged in to post a comment.