Click here to Skip to main content
15,894,312 members

Comments by MAT1003 (Top 1 by date)

MAT1003 16-Dec-14 0:44am View    
Adding Code

public void FileUpload(int? candidateID)
{
// Check to see if file was uploaded
if (FileUpload1.PostedFile != null)
{
// Get a reference to PostedFile object
HttpPostedFile myFile = FileUpload1.PostedFile;

// Get size of uploaded file
int nFileLen = myFile.ContentLength;

// make sure the size of the file is > 0
if (validatefile())
{
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];

// Read uploaded file from the Stream
myFile.InputStream.Read(myData, 0, nFileLen);

// Create a name for the file to store
string strFilename = Path.GetFileName(myFile.FileName);

// Specify a "currently active folder"
string activeDir = Server.MapPath("~/UploadedFiles/CandidateDocuments/");

//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, "Candidate_" + candidateID + "/" + dlDocumentType.SelectedItem.Text);

// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);

// Combine the new file name with the path
newPath = System.IO.Path.Combine(newPath, strFilename);

// Write data into a file
WriteToFile(newPath, ref myData);

// Store it in database
WriteToDB(strFilename, myFile.ContentType, ref myData, candidateID, dlDocumentType.SelectedItem.Text);
}
}
}
------------------------------------
<pre lang="cs">private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Create a file
FileStream newFile = new FileStream(strPath, FileMode.Create);

// Write data to the file
newFile.Write(Buffer, 0, Buffer.Length);

// Close file
newFile.Close();
}</pre>

----------------------------------------------------------
private void WriteToDB(string strName, string strType, ref byte[] Buffer, int? candidateID, string docType)
{
BusinessLogic.BusinessEntities.CandidateProfile obCandidate = new BusinessLogic.BusinessEntities.CandidateProfile();
obCandidate.Candidate_ID = candidateID;
obCandidate.FILE_NAME = strName;
obCandidate.FileSize = Buffer.Length.ToString();
obCandidate.ContentType = strType;
obCandidate.FileData = Buffer;
obCandidate.DocType = docType;
obCandidate.CREATED_BY = "Self";

obCandidate.hRMMethodName = "WriteToDB";
obCandidate.hRMPageName = "CandidateProfile";
obCandidate.hRMTabName = "Add Candidate";
obCandidate.hRMUserId = candidateID.ToString();

int? documentId = objCandidateDBA.add_candidateDocument(obCandidate);

BindCandidateDocuments(candidateID);
}