Webservice code – The method recevies a byte array from the webservice client, creates a memory stream and send it to a specific mail address, you can modify this in way so that it can save into webserver also.
[WebMethod]
public void SendLogFile(byte[] logFile, string fileName)
{
using (MemoryStream ms = new MemoryStream(logFile))
{
//This code will save the file to the uploads folder. (Note: You need to set the permission for IIS / ASP.Net user.)
using (Stream streamwriter= new Streamwriter(Path.Combine(Server.MapPath("~/Uploads"), Path.GetFileName(fileName))).BaseStream)
{
streamwriter.Write(logFile, 0, logFile.Length);
}
/*
//This code will send the uploaded file to the mail specified.
using (MailMessage msg = new MailMessage())
{
msg.To.Add(new MailAddress("toaddress@maildomain.com"));
msg.From = new MailAddress("fromaddress@maildomain.com", "File from Application");
msg.IsBodyHtml = true;
msg.Subject = "File from Application";
msg.Attachments.Add(new Attachment(ms, fileName));
SmtpClient client = new SmtpClient("mail.smtp.com");
client.Send(msg);
}
*/
}
}
Client code. – It will display a Open file dialog box, for selecting a file from Windows explorer.
localhost.Service s = new UploadApp.localhost.Service();
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Filter = "All Files|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
using (Stream str = dlg.OpenFile())
{
byte[] buffer = new byte[str.Length];
str.Read(buffer, 0, (int)str.Length - 1);
s.SendLogFile(buffer, dlg.FileName);
}
}
}