Click here to Skip to main content
15,882,063 members

Save image to web service and get its back

Omar Isaid asked:

Open original thread
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();
           }
Tags: C# 3.5, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900