Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I have a web application. The requirement is to upload the data using excel sheet with attachments.

What I have tried:

I have already completed upload data from excel sheet but I'm not able to upload the attachments files.
Example data:
---------------------------------------------------------------------------------
|CandidateID | CandidateName| Age | Picture                        | IsActive   |
---------------------------------------------------------------------------------
| 01         |   John       | 22  | C:\User\Abc\MyFiles\J123.jpg   |   Yes      |
--------------------------------------------------------------------------------

*The candidate picture path is from the client's machine.

How can I allow the client to upload data(Excel sheet) with attachment?

Can anyone please help me, how can I upload the picture file with data in asp.net C#.


Thanks in advance
Posted
Updated 9-Apr-19 1:28am
v2

You can convert the image to base64 and save it as a string.Keep the field of nvarchar(max) in datatable.

1. Convert image to base64:

string path = "D:\\SampleImage.jpg";  
    using(System.Drawing.Image image = System.Drawing.Image.FromFile(path))  
    {  
        using(MemoryStream m = new MemoryStream())  
        {  
            image.Save(m, image.RawFormat);  
            byte[] imageBytes = m.ToArray();  
            base64String = Convert.ToBase64String(imageBytes);  
            return base64String;  
        }  
    }  


2. Convert base64 string to image:


public System.Drawing.Image Base64ToImage(string base64String)   
{  
    byte[] imageBytes = Convert.FromBase64String(base64String);  
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);  
    ms.Write(imageBytes, 0, imageBytes.Length);  
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);  
    return image;  
}  
 
Share this answer
 
v2
Comments
abdul subhan mohammed 9-Apr-19 7:35am    
Dear, the image is on the Client's machine and your code shows server path?
1. You will need an HTML form with its attributes set to have a file attached:
<form action="upload.aspx" method="post" enctype="multipart/form-data">

2. Said form will need to have an input element for a file:
<input type="file" name="fileToUpload" id="fileToUpload">

3. You will need to have a server side script to process the form and save the file.
I'm not doing all the work, try Google; I did
ASP.NET - File Uploading[^]
 
Share this answer
 

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