Silverlight download file from server code#
In the above code snippet, we have three TextBoxes, a ProgressBar and a Button control. Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here. This article is the continuation of my last article in Silverlight controls series, read last article here. To demonstrate this example, we shall create a xaml file and following is my code.
Silverlight download file from server how to#
How to download file and displaying progress status? This is the first version of downloading in chunk using WCF in Silverlight project.Silverlight also allows us connect to remote machine or services, download and upload files, all these stuffs are done using WebClient class that exists into System.Net namespace.
One important design problem in such P2P systems is to determine the optimal chunk selection policy that provides high continuity playback under the server’s upload capacity constraint.Īlso, we can use MTOM web service to get the Chunks from the server.
Data-driven P2P streaming systems can potentially provide good playback rate to a large number of viewers. The selection algorithm of chunk in P2P network is handy in this case. Also the interesting point is to use downloading chunk in P2P network. We can use this method in video streaming where you have to get the data from the server and load video data into client side. Downloading in chunks is much faster and reliable. People who need to download large files can use this method. the last chunk will almost certainly not fill the buffer, // so it must be trimmed before returningīyte TrimmedBuffer = new byte Īrray.Copy(TmpBuffer, TrimmedBuffer, BytesRead) īyte DownloadChunk( String Docurl, Int64 Offset, Int32 BufferSize) read the first chunk in the buffer (which is re-used for // every chunk)īytesRead = fs.Read(TmpBuffer, 0, BufferSize) Using (FileStream fs = new FileStream(FilePath, FileMode.Open, open the file to return the requested chunk as a byte / / if the requested Offset is larger than the file, quit. Int64 FileSize = new FileInfo(FilePath).Length The WCF download in chunk function is shown below:Ĭopy Code public byte DownloadChunk( String DocUrl, Int64 Offset, Int32 BufferSize)
In the web.config, do the following settings: Once the download is completed, I close the FileStream, and display successful message to the user. You can see in the above code that I am calling WCF function again and again till it satisfies the if (I64Offset < fileSize) condition. WriteIntoFile function will write the data into the Stream. I created custom MessageBox class which is Model based. In Silverlight, to open the fileDialog, you need to call it from USER initiated event. ( " File downloaded successfully.") įor the MessageBox, see the MessageBox.xaml file. Service.DownloadChunkAsync(docName, I64Offset, i32ChunkSize) WriteIntoFile(fileDialog, byteArrayFile) " Are you sure to download the file ?", MsgBoxButtons.YesNo, MessageBox _oBox = MessageBox.Show( " Download file in chunk", Note: You have to entered valid fileSize (size of the document in Bytes) in the code.Ĭopy Code void service_DownloadChunkCompleted( object sender, DownloadChunkCompletedEventArgs e) The following function is in Silverlight page which calls the WCF function to get the Chunk data: The attached solution contains two projects, one is Silverlight and the other one is web project which contains the XAP file as well as Service class. The WCF function will be recursive so that this function will get the Chunk of data till the entire file has been downloading. You can call the WCF function and get the chunk of data and write this data into the FileStream. Let’s say you want to download the large file from your UI (which is running on client side) and the file resides at server side.
Here we are going to use Silverlight and WCF technology. This way, you can securely download the large file. Once WCF returns the Chunk data, the Silverlight application will open the FileDialog and write the data into the stream, if the file size is greater than the chunk size, the program will call the WCF function again and get the next chunk. Downloading large file in one shot is risky so the idea is from the UI (Silverlight application) user can call the WCF function to download the Chunk (let’s say 10 MB) per call. We need a functionality that can download the large file. Chunk was written to serve one purpose, and do it well: Split files into differently-sized chunks, primarily so that large files can be transported via media that is smaller than the size of the file. The chunk selection in P2P network is very crucial. Most people use chunk in video streaming and for P2P Network. You can download the large file in less time without getting any error. The project is about downloading the large file (GB) in chunks.