public class Receiver: IReceiver
{
public void SaveFile ( String filename, Int32 dataLength, Byte [] data)
{
FileStream fs = File .Open ( @ "C: \ Temp \" + filename, FileMode.Append);
fs.write (data, 0, dataLength);
fs.Close ();
}
public void DeleteIfExists (String filename)
{
if (File.Exists (@ " C: \ Temp \ " + filename)) File.Delete (@ " C: \ Temp \" + filename);
}
}
< services >The reason for this is that Silverlight applications can work only with basicHttpBinding "binding" (for now or so it will always be - not known).
< service behaviorConfiguration = "ReceiverBehavior" name = "Receiver" >
< endpoint address = "" binding = "basicHttpBinding" contract = "IReceiver" >
< identity >
< dns value = "localhost" />
</ identity >
</ endpoint >
< endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange" />
</ service >
</ services >
< UserControl x: Class = “UploadProgress.Page”This code describes the StackPanel, which contains a button and a block of text. At the button, the OnBrowseClick handler is hung on the Click event.
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "30" >
< StackPanel Orientation = "Horizontal" >
< Button x: Name = "btnBrowse" Content = "Browse"
Width = "100" Height = "25" Click = "OnBrowseClick" />
< TextBlock x: Name = "lblProgress"
Text = "Please select file ..." Margin = "5" />
</ StackPanel >
</ UserControl >
private ReceiverClient _receiver; // our WCF service created earlierNow we describe the event handlers. When you click on a button, the OnBrowseClick handler is called first.
private String _fileName; // name of the file being uploaded
private Stream _fileData; // file stream of the uploaded file
private Int64 _dataLength; // length of transmitted data
private Int64 _dataSent; // length of sent data
private void OnBrowseClick (Object sender, RoutedEventArgs e)As a result of executing the delete function, the OnDeleteIfExistsCompleted handler is called:
{
var dlg = new OpenFileDialog (); // File Selection Dialog
dlg.Multiselect = false ; // Disable multiple file selection
dlg.Filter = "All Files | *. *" ; // Set Filter On Files
if ((Boolean) dlg.ShowDialog ()) // show the file selection dialog
{
_fileName = dlg.SelectedFile.Name; // name of the file being uploaded
_fileData = dlg.SelectedFile.OpenRead (); // file stream
_dataLength = _fileData.Length; // length of transmitted data
_dataSent = 0; // length of the transferred data
_receiver = new ReceiverClient (); // Receiver (web-service)
// file deletion event
_receiver.DeleteIfExistsCompleted + = OnDeleteIfExistsCompleted;
// event of saving a chunk of data
_receiver.SaveFileCompleted + = OnSaveFileCompleted;
// call the file deletion function
// in case the file with the same name already exists
// as a result, the handler will be called
// OnDeleteIfExistsCompleted
_receiver.DeleteIfExistsAsync (_fileName);
}
}
private void OnDeleteIfExistsCompleted (Object sender, AsyncCompletedEventArgs e)This function does nothing except what the OnSaveFileCompleted handler calls. The OnSaveFileCompleted handler reads a portion of the data from the file stream and calls the send function SaveFileAsync, which causes the same OnSaveFileCompleted processor to be called. Recursion continues until the entire file is transferred, after which the OnUploadCompleted method is called.
{
OnSaveFileCompleted (sender, e);
}
private void OnSaveFileCompleted (Object sender, AsyncCompletedEventArgs e)The length is 4 * 4096 bytes - chosen by me arbitrarily. Perhaps there is a more optimal length, but so far. And the last.
{
Byte [] buffer = new Byte [4 * 4096];
Int32 bytesRead = _fileData.Read (buffer, 0, buffer.Length);
if (bytesRead! = 0)
{
_receiver.SaveFileAsync (_fileName, bytesRead, buffer);
_dataSent + = bytesRead;
// notify about the next load of data
OnProgressChanged ();
}
else
{
// all data loaded
OnUploadCompleted ();
}
}
private void OnProgressChanged ()That's all. Download sources for VS2008 SP1 and Silverlight b2b here .
{
// display the current progress of the file download
lblProgress. Text = String. Format ( "{0} / {1}" , _dataSent, _dataLength);
}
private void OnUploadCompleted ()
{
// inform about the end of the download
lblProgress. Text = "Complete!" ;
}
Source: https://habr.com/ru/post/38296/
All Articles