Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#3.5
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
 
 
 [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 24-Mar-13 4:08am
Comments
Prasad Khandekar at 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 Adnan Isaid at 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 at 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)).
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
 [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" />
  Permalink  
v2
Comments
Omar Adnan Isaid at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 197
4 Sinisa Hajnal 176
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 31 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100