Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Upload and Save Image Using Webservice

Upload and Save Image Using Webservice

Scheduled Pinned Locked Moved C#
helpcsharpjavascriptasp-netlinq
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, I have a web page that allows users to drag an image from their desktop on to a box that had been designated as a droppable box. Once the image is dropped it will be sent to a web service to be saved in a folder on the server. The following is the Javascript I'm using to send the image from the frontend to the webservice.

    function ImageUpload(file) {
    xhr = new XMLHttpRequest();
    xhr.open('post', 'SaveImageService.asmx', true);
    xhr.setRequestHeader('Content-Type', "multipart/form-Data");
    xhr.setRequestHeader('X-File-Name', file.fileName);
    xhr.setRequestHeader('X-File-Size', file.fileSize);
    xhr.setRequestHeader('X-File-type', file.fileType);
    xhr.send(file);
    };

    I am using the following code in my web service to save the posted image in a folder called Images. I'm not certain if the project folder name should be included as part of the folder path when I specify where to save the uploaded images. I tried including and excluding the project folder name but it didn't help.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Imaging;

    namespace ImageUpload
    {
    /// /// Summary description for SaveImageService
    ///
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class SaveImageService : System.Web.Services.WebService
    {

        \[WebMethod\]
        public void SaveImage()
        {
            var httpRequest = HttpContext.Current.Request;
            foreach (string file in httpRequest.Files)
            {
                var postedFile = httpRequest.Files\[file\];
                var filePath = HttpContext.Current.Server.MapPath("~/Images/" + postedFile.FileName);
                postedFile.SaveAs(filePath);
            }
        }
    }
    

    }

    I am getting response status of 500 internal server error. Please look at the code above and point out any problem you see. Thanks in advance for your help.

    C N 2 Replies Last reply
    0
    • A ASPnoob

      Hi all, I have a web page that allows users to drag an image from their desktop on to a box that had been designated as a droppable box. Once the image is dropped it will be sent to a web service to be saved in a folder on the server. The following is the Javascript I'm using to send the image from the frontend to the webservice.

      function ImageUpload(file) {
      xhr = new XMLHttpRequest();
      xhr.open('post', 'SaveImageService.asmx', true);
      xhr.setRequestHeader('Content-Type', "multipart/form-Data");
      xhr.setRequestHeader('X-File-Name', file.fileName);
      xhr.setRequestHeader('X-File-Size', file.fileSize);
      xhr.setRequestHeader('X-File-type', file.fileType);
      xhr.send(file);
      };

      I am using the following code in my web service to save the posted image in a folder called Images. I'm not certain if the project folder name should be included as part of the folder path when I specify where to save the uploaded images. I tried including and excluding the project folder name but it didn't help.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Services;
      using System.IO;
      using System.Drawing;
      using System.Drawing.Imaging;

      namespace ImageUpload
      {
      /// /// Summary description for SaveImageService
      ///
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [System.ComponentModel.ToolboxItem(false)]
      // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
      [System.Web.Script.Services.ScriptService]
      public class SaveImageService : System.Web.Services.WebService
      {

          \[WebMethod\]
          public void SaveImage()
          {
              var httpRequest = HttpContext.Current.Request;
              foreach (string file in httpRequest.Files)
              {
                  var postedFile = httpRequest.Files\[file\];
                  var filePath = HttpContext.Current.Server.MapPath("~/Images/" + postedFile.FileName);
                  postedFile.SaveAs(filePath);
              }
          }
      }
      

      }

      I am getting response status of 500 internal server error. Please look at the code above and point out any problem you see. Thanks in advance for your help.

      C Offline
      C Offline
      CAReed
      wrote on last edited by
      #2

      As you are calling this using AJAX, then do you have the SaveImageService registered in the ScriptManager control on your ASPX page?

      Christopher Reed "The oxen are slow, but the earth is patient."

      A 2 Replies Last reply
      0
      • C CAReed

        As you are calling this using AJAX, then do you have the SaveImageService registered in the ScriptManager control on your ASPX page?

        Christopher Reed "The oxen are slow, but the earth is patient."

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        The web service is not registered in the scriptmanager control and there is no aspx page.

        1 Reply Last reply
        0
        • C CAReed

          As you are calling this using AJAX, then do you have the SaveImageService registered in the ScriptManager control on your ASPX page?

          Christopher Reed "The oxen are slow, but the earth is patient."

          A Offline
          A Offline
          ASPnoob
          wrote on last edited by
          #4

          Hi, thanks for responding. The web service is not registered in the Scriptmanager control and there is no ASXP page.

          1 Reply Last reply
          0
          • A ASPnoob

            Hi all, I have a web page that allows users to drag an image from their desktop on to a box that had been designated as a droppable box. Once the image is dropped it will be sent to a web service to be saved in a folder on the server. The following is the Javascript I'm using to send the image from the frontend to the webservice.

            function ImageUpload(file) {
            xhr = new XMLHttpRequest();
            xhr.open('post', 'SaveImageService.asmx', true);
            xhr.setRequestHeader('Content-Type', "multipart/form-Data");
            xhr.setRequestHeader('X-File-Name', file.fileName);
            xhr.setRequestHeader('X-File-Size', file.fileSize);
            xhr.setRequestHeader('X-File-type', file.fileType);
            xhr.send(file);
            };

            I am using the following code in my web service to save the posted image in a folder called Images. I'm not certain if the project folder name should be included as part of the folder path when I specify where to save the uploaded images. I tried including and excluding the project folder name but it didn't help.

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.Services;
            using System.IO;
            using System.Drawing;
            using System.Drawing.Imaging;

            namespace ImageUpload
            {
            /// /// Summary description for SaveImageService
            ///
            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            [System.ComponentModel.ToolboxItem(false)]
            // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
            [System.Web.Script.Services.ScriptService]
            public class SaveImageService : System.Web.Services.WebService
            {

                \[WebMethod\]
                public void SaveImage()
                {
                    var httpRequest = HttpContext.Current.Request;
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files\[file\];
                        var filePath = HttpContext.Current.Server.MapPath("~/Images/" + postedFile.FileName);
                        postedFile.SaveAs(filePath);
                    }
                }
            }
            

            }

            I am getting response status of 500 internal server error. Please look at the code above and point out any problem you see. Thanks in advance for your help.

            N Offline
            N Offline
            Nathan Minier
            wrote on last edited by
            #5

            I think your issue is in your loop. Try:

            foreach (string file in httpRequest.Files.AllKeys)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups