------------------To Create Handler------------------------
AddNewItem---->GenericHandler--->
---------in .js file---------------
function loadimageUpload() {
var Imagepath = $('#ucID_hItemImage').val();
$('#itemImage').attr("src", Imagepath);
var button = $('#browsebtn'), interval;
$.ajax_upload(button, {
action: 'Handlers/UploadeItemImagesHandlers.ashx',
name: 'myfile',
type: 'GET',
onSubmit: function (file, ext) {
// change button text, when user selects fil
if (ext.toLowerCase() == "jpg" || ext.toLowerCase() == "jpeg" || ext.toLowerCase() == "png" || ext.toLowerCase() == "tiff" || ext.toLowerCase() == "gif") {
button.text('Uploading');
interval = window.setInterval(function () {
var text = button.text();
if (button.text().length < 13) {
button.text(button.text() + '.');
} else {
button.text('Uploading');
}
}, 200);
}
else {
//alert('Please Select Valid Image');
alert(Default.getScriptMessageFromDb(76).value);
return false;
}
},
data: { method: 'GreetMe', args: { name: "gopi" } },
success: function (data) {
alert(data);
},
onComplete: function (file, response) {
response = response.replace(/"/gi, "");
button.text('Upload');
var zip = 'responseText';
if (file != "Invalid" && response != "Invalid") {
$('#itemImage').attr("src", 'UploadedFiles/Items/' + response); //store image in folder
$('#ucID_hItemImage').val('UploadedFiles/Items/' + response);
button.removeClass('hover');
window.clearInterval(interval);
}
else {
//alert('Error in Saving Image');
alert("Please Select Valid Image(.jpg,.jpeg,.png,.tiff,.gif)");
}
// add file to the list
}
});
}
----------To create Generic Handler--------
AddNewItem-->select Generic Handler(.ashx)
----------in .ashx file------------
public class UploadeItemImagesHandlers : IHttpHandler{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string strResponse = "error";
string strSaveLocation = "";
try
{
string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
string ImageFilePath = DateTime.Now.Ticks.ToString() + strExtension;
if (strExtension == ".jpg" || strExtension == ".jpeg" || strExtension == ".png" || strExtension == ".tiff" || strExtension == ".gif")
{
strSaveLocation = context.Server.MapPath("~/UploadedFiles/Items");
string filename = Path.Combine(strSaveLocation, ImageFilePath);
HttpPostedFile oFile = context.Request.Files[0];
oFile.SaveAs(filename);
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
// context.Request.Files[0].SaveAs(strSaveLocation);
strResponse = javaScriptSerializer.Serialize(ImageFilePath);
}
else
{
strResponse = "Invalid";
}
}
catch (Exception Ex)
{
strResponse = Ex.Message;
}
context.Response.Write(strResponse);
}
public bool IsReusable
{
get
{
return false;
}
}
}
No comments:
Post a Comment