Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to upload resume in server using C# application using online database
Posted

TS mentioned 'resume'. The solution posted before uploads and stores the file successfully, but does not support a resume scenario.

In order to support resume, make the following changes:

- Check if a record for the filename already exists
- Take the length of the existing binary data as starting point, or store the length in a separate field for performance reasons
- Read the file contents from the starting point using a loop
- Append to the existing data and update it in the database in the loop

This way, whenever the upload is aborted/cancelled, the chunks that were already processed will always be stored in the database.

In addition, I would add a flag to the table to indicate if the upload has completed or if the record contains partial data from the uploaded file.

If you need a code sample, let me know.
 
Share this answer
 
Comments
Suvendu Shekhar Giri 26-Apr-20 6:34am    
+5 for the approach.
C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    // Read the file and convert it to Byte Array
    string filePath = FileUpload1.PostedFile.FileName;  
    string filename = Path.GetFileName(filePath);
    string ext = Path.GetExtension(filename);
    string contenttype = String.Empty;
 
    //Set the contenttype based on File Extension
    switch(ext)
    {
        case ".doc":
            contenttype = "application/vnd.ms-word";
            break;
        case ".docx":
            contenttype = "application/vnd.ms-word";
            break;        
        case ".pdf":
            contenttype = "application/pdf";
            break;
    }
    if (contenttype != String.Empty)
    {
 
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
 
        //insert the file into database
        string strQuery = "insert into tblFiles(Name, ContentType, Data)" +
           " values (@Name, @ContentType, @Data)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
        cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
          = contenttype;
        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
        InsertUpdateData(cmd);
        lblMessage.ForeColor = System.Drawing.Color.Green;  
        lblMessage.Text = "File Uploaded Successfully";
    }
    else
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;   
        lblMessage.Text = "File format not recognised." +
          " Upload Image/Word/PDF formats";
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900