≡ Menu




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;

}

else

{

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);

newFile.Close();

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

}

// Read file into a data stream

//return sFilename;

}

{ 0 comments… add one }

Leave a Comment