Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Respected


I am developing a module in asp.net. Where a user submits a picture using fileupload control to the server. At server i Have to create image of the posted file without saving it on server. Then i have to save it to sql server database not on server. So help is needed

To Some Extent I have coded some of the portion but the major portion is to be done
C#
Stream fi = FileUpload1.PostedFile.InputStream;
var ext = FileUpload1.PostedFile.ContentType;
var imageOrNot = ext.Contains("image") ? true : false;

if (imageOrNot)
{
      byte[] imagearr = new byte[fi.Length];
      fi.Read(imagearr, 0, (int)fi.Length);
      fi.Close();
      Response.Write(imagearr);
      MemoryStream ms = new System.IO.MemoryStream(imagearr);
      System.Drawing.Image imgTemp = System.Drawing.Image.FromStream(ms);
 
     FileStream fs = File.OpenRead(FileUpload1.PostedFile.FileName);
     //byte[] ba = new byte[fs.Length];
     //int length = fs.Read(ba, 0, ba.Length);

Thanks in Advance
Posted

Letz assume DB field name is EmployeeImg with image attribute of SQL Server. Code will be:
C#
SqlConnection connection = null;
  try
  {
      FileUpload img = (FileUpload)imgUpload;
      Byte[] imgByte = null;
      if (img.HasFile && img.PostedFile != null)
      {
          HttpPostedFile File = imgUpload.PostedFile;
          imgByte = new Byte[File.ContentLength];
          File.InputStream.Read(imgByte, 0, File.ContentLength);
      }
      string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
      connection = new SqlConnection(conn);

      connection.Open();
     string sql = "INSERT INTO EmpDetails(EmployeeImg) VALUES(@EmployeeImg)";
      SqlCommand cmd = new SqlCommand(sql, connection);
      cmd.Parameters.AddWithValue("@EmployeeImg", imgByte);
      int id = Convert.ToInt32(cmd.ExecuteScalar());
  }
 
Share this answer
 
Comments
TanzeelurRehman 11-Jan-12 0:59am    
Ok, Some of the problem got solved, when i get the image bytes from the server, how can i display them on webpage.
 
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