Anda di halaman 1dari 2

using System;

using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class tutorials_controls_tutorialtemplate_fileupload : System.Web
.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string keyWord = "asp:FileUpload Control, FileUpload Control, ASP.NET Fi
leUpload Control, asp.net standard controls";
string desc = "How to use FileUpload control in asp.net? Working with Fi
leUpload control in ASP.NET";
impFunctions.WriteMetaTags(this, keyWord, desc, false);
}
/// <summary>
/// Fires when Button is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UploadFileOnServer(object sender, EventArgs e)
{
// limitation of maximum file size
int intFileSizeLimit = 10;
// get the full path of your computer
string strFileNameWithPath = FileUpload1.PostedFile.FileName;
// get the extension name of the file
string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPat
h);
// get the filename of user file
string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);
// get the file size
int intFileSize = FileUpload1.PostedFile.ContentLength / 1024; // conver
t into byte
// Restrict the user to upload only .gif or .jpg file
strExtensionName = strExtensionName.ToLower();
if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif"))
{
// Rstrict the File Size
if (intFileSize < intFileSizeLimit)
{
// upload the file on the server
// you can save the file with any name, However as this is the s
ample so I have saved the file same name for all users. So it will overwrite you
r file with next user's who will test this tutorials.
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UserFiles/Sample
s/") + "SampeFromTutorials" + strExtensionName);
lblMessage.Text = "Uploaded file details <hr />" +
"File path on your Computer: " + strFileNameWithPath + "<br
/>" +
"File Name: " + strFileName + "<br />" +
"File Extension Name: " + strExtensionName + "<br />" +
"File Size: " + intFileSize.ToString();
}
else
{
lblMessage.Text = "File size exceeded than limit " + intFileSize
Limit + " KB, Please upload smaller file.";
}
}
else
{
lblMessage.Text = "Only .jpg or .gif file are allowed, try again!";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}

Anda mungkin juga menyukai