Click here to Skip to main content
15,910,980 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i have a form to save employee information including employee image,image is saving properly using following code.
C#
Employee.ImgBody = (Employee.ImgBody == null) ? new byte[0] : Employee.ImgBody;
               //Image saving code
               Byte[] imgByte = null;
               if (txtEpPicture.HasFile && txtEpPicture.PostedFile != null)
               {
                   //To create a PostedFile
                   HttpPostedFile File = txtEpPicture.PostedFile;
                   //Create byte Array with file len
                   imgByte = new Byte[File.ContentLength];
                   //force the control to load data in array
                   File.InputStream.Read(imgByte, 0, File.ContentLength);
                   Employee.ImgBody = imgByte;
               }

now i want to show that image when user select any employee and also want to update that image if user need. i want to show image into the form by using following code
C#
empPicture.ImageUrl = string.Format("EmpPicture.ashx?id={0}", Employee.Id);

i have search and try different solution from the google,but all in vain,image is in db in the form of varbinary.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Apr-15 17:32pm    
Not clear. Web form or System.Windows.Forms.Form? Is it ASP.NET (HttpPostedFile, probably Image.ImageUrl...)? If so, do you want to present picture dynamically, in the fly, not saving posted file on the server side, or you want to show a file? You cannot read a file from input stream, do nothing with the result and expect it's shown via URL. The problem is really simple, you just need to understand what you are doing.
—SA
Sajid227 10-Apr-15 17:45pm    
web form ,i want to save it first ,but when getting employee data for update purpose then image also show up on the form and can be replaced
Sergey Alexandrovich Kryukov 10-Apr-15 17:53pm    
This "issue" is not related to Web forms... Please see my answer.
—SA
Sajid227 10-Apr-15 18:00pm    
then why image is not displaying on the form when i retrieve the information about that employee
Sergey Alexandrovich Kryukov 10-Apr-15 18:13pm    
Because you are not doing anything to display it. I don't see your Employee definition to see what you are really doing, I'm not sure that Employee.Id gets correct URL. Just the opposite, it's possible that this is nothing by id attribute, that is, something irrelevant. But it does not matter. You already are trying to make things more complex than they have to be. Showing an uploaded image is a really simple technique. Again, please see my answer.
—SA

1 solution

This "issue" is not related to Web forms at all. It is only related to the HTML use for uploading: you can use <input type="file"/>; and it has to be in some Web form, with "Sumbit" button (type="submit"; for basic information, please see: http://www.w3schools.com/html/html_forms.asp).

This is a minimal solution, one one of the most practically reasonable at the same time:
HTML
<img src="myUploadedImage.png"/>

Now, follow this simple logic: by all counts, you already have server-side processing and handle uploads. If you wanted to avoid saving a file in your server-side file system, it could be a bit more complex, but you don't want it, you want to save the file. If you wanted to do some post-processing of file before presentation, it could be a bit more complex, but apparently you are not doing anything like that. But then you don't need to read anything from HTTP request and do any processing except saving the file under certain file name and referencing this file name in the <img> element of your HTML generated in your HTTP response, no matter how you do it.

There are a lot of articles written on handling uploads. Let's consider one ASP.NET example: you can use the class System.Web.UI.WebControls.FileUpload:
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload%28v=vs.110%29.aspx,
and, for example, http://asp.net-tutorials.com/controls/file-upload-control.

Pay attention form the SaveAs call in the code sample references above. This is all you need, no stream reading, no other processing; everything is already done by ASP.NET.
If you use something else, the idea will be exactly the same.

—SA
 
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