ali raza

C#, .Net , and my thoughts

Code to upload file in c#

protected void btnUploadFile_Click(object sender, EventArgs e){

string sFilename = “”;// Check file size (mustn’t be 0)

HttpPostedFile myFile = FileUploading.PostedFile;int nFileLen = myFile.ContentLength;

if (nFileLen==0){

lblMsg.Text=“No valid file for upload.”;// return;




byte[] myData=new Byte[nFileLen];myFile.InputStream.Read(myData, 0, nFileLen);

sFilename=System.Guid.NewGuid().ToString().Replace(“-“, “”)+“_”+System.IO.Path.GetFileName(myFile.FileName);// Save the stream to disk

System.IO.FileStream newFile=new System.IO.FileStream(Server.MapPath(TaskManagementTool.Config.Path+sFilename), System.IO.FileMode.Create);newFile.Write(myData, 0, myData.Length);


TaskManagementTool.TaskDB taskDB=new TaskManagementTool.TaskDB();taskDB.AddUploadedFile(Convert.ToInt32(lblTaskId.Text), sFilename, ckIsPrivate.Checked);ShowUploadFiles();


// Read file into a data stream

//return sFilename;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: