Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building an online book store , the upload of the pictures of the books shall be through XML web service
Now I found the code to save the image at the web service file system but The Problem is
1 - how to Build a method to get back the url of the image or the image it self
2 - how to Get the image or the image url and set to asp.net image control or other suitable control

below is the method at the web service accept parameters : 1- the image in byte array form and the 2 - file name , this method save the picture in folder called "TransientStorage" folder

C#
[WebMethod]
       public string UploadFile(byte[] f, string fileName)
       {
           // the byte array argument contains the content of the file
           // the string argument contains the name and extension
           // of the file passed in the byte array
           try
           {
               // instance a memory stream and pass the
               // byte array to its constructor
               MemoryStream ms = new MemoryStream(f);

               // instance a filestream pointing to the
               // storage folder, use the original file name
               // to name the resulting file
               FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
                           ("~/TransientStorage/") + fileName, FileMode.Create);

               // write the memory stream containing the original
               // file as a byte array to the filestream
               ms.WriteTo(fs);

               // clean up
               ms.Close();
               fs.Close();
               fs.Dispose();

               // return OK if we made it this far
               return "OK";
           }
           catch (Exception ex)
           {
               // return the error message if the operation fails
               return ex.Message.ToString();
           }
Posted
Comments
Prasad Khandekar 24-Mar-13 10:53am    
Why not return the file URL instead of "OK".Or you can even return a JSON object something which looks like {"Status" : "OK", "ImageUrl" : "Your Image Relative Path"}. Your service method any way knows the path where image is getting stored.
Omar Isaid 24-Mar-13 13:12pm    
How i can get the physical and relative paths of i know only the file and folder names
Prasad Khandekar 24-Mar-13 14:12pm    
Sending physical path might not help. However if your image storage folder is inside your application web root then relative path will allow your client side javascript code to show it immediately if required by either changing the src attribute of the placeholder image tag or by adding a fresh img tag. You wont be able to update any server side control as your service method is directly sending a response to javascript. To achieve this you may want to store the return value of System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") + filename in Session and issue a GET immediately from your client side code. (Window.replace(PAGE URL)).

Hey Omar,

If i got your problem right, here is the solution.
Provided you have'nt kept any security check at the web-service.

Quote:
how to Build a method to get back the url of the image or the image it self


You can get back the image URL by returning it from Upload file method, instead of status.
And check its status through string functions or regular expressions.

C#
[WebMethod]
       public string UploadFile(byte[] f, string fileName)
       {
           // the byte array argument contains the content of the file
           // the string argument contains the name and extension
           // of the file passed in the byte array
           try
           {
               // instance a memory stream and pass the
               // byte array to its constructor
               MemoryStream ms = new MemoryStream(f);

               // instance a filestream pointing to the
               // storage folder, use the original file name
               // to name the resulting file

               string imgURL=System.Web.Hosting.HostingEnvironment.MapPath
                           ("~/TransientStorage/") + fileName;

               FileStream fs = new FileStream(imgURL, FileMode.Create);

               // write the memory stream containing the original
               // file as a byte array to the filestream
               ms.WriteTo(fs);

               // clean up
               ms.Close();
               fs.Close();
               fs.Dispose();

               // return OK if we made it this far
               return imgURL;
           }
           catch (Exception ex)
           {
               // return the error message if the operation fails
               return ex.Message.ToString();
           }


Quote:
how to Get the image or the image url and set to asp.net image control or other suitable control


Once you get the img URL, you can set it to image control or simply an HTML image tag,

<img src="imgURL" />
 
Share this answer
 
v2
Comments
Omar Isaid 24-Mar-13 13:10pm    
Thank you , but how to get the physical path if i have only the file and directory names and if i can get the physical path how i can set as source for Image asp.net control
Monster Maker 24-Mar-13 16:39pm    
See, Once you got your physical path, initialize the ImgURL propert of Image asp.net control
with the string returned by the function. It would work.

Now the question is about getting the physical path, you can do this through a GET request from the client side or through ajax(preferred), I can't explain it in detail as i have no more details about your project.
This problem can be solved in two different ways :
1- Virtual directory where the web service can save the images and the ASP.NET application can ge the virtual path of the images at the virtual directory and display at the aspx page
2- Web sevice save the images at a directory and a generic handler at the ASP.NET application get the images as array of bytes to be displayed at aspx page

The second solution available at the below Tip & Trick

Submit Images to Web Service and Get Them Back[^]
 
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