Upload and Save Image Using Webservice
-
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.
-
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.
-
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."
-
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."
-
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.
I think your issue is in your loop. Try:
foreach (string file in httpRequest.Files.AllKeys)