Kamran Ahmed

Uploading the file in Asp.net




1.     Create one empty folder in ur project say having name “Uploads”.
2.     The following code example demonstrates how to create a FileUpload control that saves files to a path that is specified in code. The SaveAs method is called to save the file to the specified path on the server. The ASP.NET application that includes the example must have write access to the specified directory on the server. You can explicitly grant write access to the account under which the application is running, in the directory in which the uploaded files will be saved. Alternatively, you can increase the level of trust that is granted to the ASP.NET application.
protected void UploadButton_Click(object sender, EventArgs e)
  {
        Boolean fileOK = false;
        String path = Server.MapPath("~/Uploads/");
        if (FileUpload1.HasFile)
        {
            // Get the size in bytes of the file to upload.
            int fileSize = FileUpload1.PostedFile.ContentLength;

            // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded.
            if (fileSize < 2100000)
            {

                String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = {".gif", ".png", ".jpeg", ".jpg"};
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                   if (fileExtension == allowedExtensions[i])
                   {
                        fileOK = true;
                   }
                }
             }
        }

        if (fileOK)
        {
            try
            {
                FileUpload1.PostedFile.SaveAs(path
                    + FileUpload1.FileName);
                Label1.Text = "File uploaded!";
            }
            catch (Exception ex)
            {
                Label1.Text = "File could not be uploaded.";
            }
        }
        else
        {
            Label1.Text = "Cannot accept files of this type.";
        }
    }


  }


Note:- The maximum size file that can be uploaded depends on the value of the MaxRequestLength configuration setting. If users attempt to upload a file that is longer than the maximum, the upload fails.

DownLoading the file

private void Button2_Click(object sender, System.EventArgs e)
{
string sFileName = Server.MapPath("~\\Uploads" + "\\HHHHWHuman genome1.doc") ;
FileInfo objF = new FileInfo(sFileName);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + objF.Name);
Response.AddHeader("Content-Length", objF.Length.ToString());
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.WriteFile(objF.FullName);
}
How to send the mail

using System.Net.Mail;
using System.Net.Mime;
private void btnMail_Click(object sender, System.EventArgs e)
{
      // Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the current working directory.
      string file = "data.xls";
      // Create a message and set up the recipients.
MailMessage message = new MailMessage("from@contoso.com","to@contoso.com",              "Subject goes here.", "mail body message.");

objMailMessage.Bcc.Add(txtBcc.Text);
                objMailMessage.CC.Add(txtCc.Text);

      // Create  the file attachment for this e-mail message.
      Attachment data = new Attachment(file);
      // Add the file attachment to this e-mail message.
      message.Attachments.Add(data);
      //Send the message.
      SmtpClient client = new SmtpClient("ServerName");
      // Add credentials if the SMTP server requires them.
      client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
      client.Send(message);
//or u can use the
//client.Send("from@yahoo.com", "To@yahoo.com", "test message", "this is message");
      data.Dispose();  
}

No comments:

Post a Comment