1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| namespace ModuleUpload_05062010
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void b_recherche_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)|*.*|PNG Images (*.png)|*.png";
bool? retval=dlg.ShowDialog(); /* ouverture de la CFileDialog */
if (retval != null && retval == true) /* l'utilisateur à cliqué sur OK */
{
textBox_cheminFichier.Text = dlg.File.Name;
UploadFile(dlg.File.Name, dlg.File.OpenRead());
}
else
{
textBox_cheminFichier.Text = "No file selected...";
}
}
private void UploadFile(string fileName, Stream data)
{
UriBuilder ub = new UriBuilder("http://localhost:33231/receiver.ashx");
ub.Query = string.Format("filename={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
try
{
c.OpenWriteAsync(ub.Uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
} |
Partager