Multiple file upload


This article deals with multiple file upload using SWF upload,A tool for multiple image uploads SWF is a Java script Library that uses the Flash Player's upload function which includes file size checking and uploading progress

Multiple File Upload


This article deals with multiple file upload using SWF upload,A tool for multiple image uploads
SWF is a Java script Library that uses the Flash Player's upload function which includes file size checking and uploading progress
The file upload can be done by using the following java script code as follows

var swfu;
function fnUpload() {
swfu = new SWFUpload({
// Backend Settings
upload_url: "uploadMultipleImages.aspx?QuStr=photos",
post_params: {
"QuStr": "photos"
},

// File Upload Settings
file_size_limit: "10 MB",
file_types: "*.jpg;*.gif;*.bmp;*.jpeg",
file_types_description: "JPG Images",
file_upload_limit: "0", // Zero means unlimited

// Event Handler Settings - these functions as defined in Handlers.js
// The handlers are not part of SWFUpload but are part of my website and control how
// my website reacts to the SWFUpload events.
file_queue_error_handler: fileQueueError,
file_dialog_complete_handler: fileDialogComplete,
upload_progress_handler: uploadProgress,
upload_error_handler: uploadError,
upload_success_handler: uploadSuccess,
upload_complete_handler: uploadComplete,

// Button settings
button_image_url: "images/XPButtonNoText_160x22.png",
button_placeholder_id: "spanButtonPlaceholder",
button_width: 160,
button_height: 22,
button_text: 'Upload',
button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
button_text_top_padding: 1,
button_text_left_padding: 5,

// Flash Settings
flash_url: "photos/swfupload/swfupload.swf", // Relative to this file

custom_settings: {
upload_target: "divFileProgressContainer"
},

// Debug Settings
debug: false
});
}


Handler



This javascript file handles the displaying of uploaded images and displaying the upload progress removal of images etc
The Image upload as follows, the file uploadMultipleImages.aspx?QuStr=photos is referred for uploading the images and this file is responsible
for creating three sets of files ranging from small,large,thumbnail and the actual size of the image

public void UploadPhoto()
{
string sAlbumname = "";
sAlbumname = "photos";
try
{
string photocount = Request.QueryString["hPhotoTitlecount"];

XmlDocument xDoc = new XmlDocument();
string sFileName = "";

sFileName = Request.Files[0].FileName;

string sImagContentType = Request.Files[0].ContentType.ToLower();

if (sImagContentType.Contains(";"))
sImagContentType = sImagContentType.Substring(0, sImagContentType.IndexOf(';'));

string sImgFName = Request.Files[0].FileName + "";

bool isValidFileName = false;

Regex r = new Regex("^([^\\.]+)+(\\.(?i)(gif|jpeg|pjpeg|jpg|bmp|png))$");
if (r.IsMatch(sImgFName))
isValidFileName = true;

sFileName = sFileName.Replace(",", "-");
System.Drawing.Image iReqImage1 = System.Drawing.Image.FromStream(Request.Files[0].InputStream);

string pathPhotoDir = "";
if (ConfigurationManager.AppSettings.Get("sharephotosupload") != null)
pathPhotoDir = ConfigurationManager.AppSettings.Get("sharephotosupload").ToString();

if (isValidFileName)
{
string sFileExt = sFileName;
string sFileforName = "";
//sFileExt = sFileExt.Substring(sFileExt.LastIndexOf('.'));
sFileExt = ".jpg";

if (sAlbumname.Length > 250)
sAlbumname = sAlbumname.Substring(0, 250);
string file = pathPhotoDir + "original" + sFileName;

sFileforName = GetSafeFileName(sAlbumname, pathPhotoDir, sFileExt);
sFileforName = Regex.Replace(sFileforName, @"\s+", "-");
string basePath = pathPhotoDir;

DirectoryInfo basedir = new DirectoryInfo(pathPhotoDir);
if (basedir.Exists == false)
basedir.Create();

DirectoryInfo originaldir = new DirectoryInfo(basedir + "original");
if (originaldir.Exists == false)
originaldir.Create();

DirectoryInfo thumbnailfull = new DirectoryInfo(basedir + "thumbnailfull");
if (thumbnailfull.Exists == false)
thumbnailfull.Create();

DirectoryInfo thumbnail = new DirectoryInfo(basedir + "thumbnail");
if (thumbnail.Exists == false)
thumbnail.Create();

DirectoryInfo large = new DirectoryInfo(basedir + "large");
if (large.Exists == false)
large.Create();

DirectoryInfo small = new DirectoryInfo(basedir + "small");
if (small.Exists == false)
small.Create();

iReqImage1.Save(basePath + "original\\" + sFileforName);

if (iReqImage1.Width >= iReqImage1.Height)
{

System.Drawing.Bitmap ImgOriginal = new System.Drawing.Bitmap(iReqImage1);

System.Drawing.Bitmap bThumb = new System.Drawing.Bitmap((iReqImage1.Width * 170 / iReqImage1.Height), 170);
System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bThumb);
try
{
gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gThumb.DrawImage(originalImage, 0, 0, (iReqImage1.Width * 170 / iReqImage1.Height), 170);
bThumb.Save(basePath + "thumbnail\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gThumb.Dispose();
bThumb.Dispose();
originalImage.Dispose();
// iReqImage1.Dispose();
}
}
else if (iReqImage1.Height > iReqImage1.Width)
{
System.Drawing.Bitmap ImgOriginal = new System.Drawing.Bitmap(iReqImage1);

System.Drawing.Bitmap bThumb = new System.Drawing.Bitmap(150, (iReqImage1.Height * 150 / iReqImage1.Width));
System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bThumb);
try
{
gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gThumb.DrawImage(originalImage, 0, 0, 150, (iReqImage1.Height * 150 / iReqImage1.Width));
bThumb.Save(basePath + "thumbnail\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gThumb.Dispose();
bThumb.Dispose();
originalImage.Dispose();
// iReqImage1.Dispose();
}
}

if (iReqImage1.Width >= 770)
{
System.Drawing.Bitmap bThumbnailfull = new System.Drawing.Bitmap(600, (iReqImage1.Height * 600 / iReqImage1.Width));
System.Drawing.Bitmap originalImage2 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gThumbnailfull = System.Drawing.Graphics.FromImage(bThumbnailfull);
try
{
gThumbnailfull.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gThumbnailfull.DrawImage(originalImage2, 0, 0, 600, (iReqImage1.Height * 600 / iReqImage1.Width));
bThumbnailfull.Save(basePath + "thumbnailfull\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gThumbnailfull.Dispose();
bThumbnailfull.Dispose();
originalImage2.Dispose();
//iReqImage1.Dispose();
}
}
else
{
iReqImage1.Save(basePath + "thumbnailfull\\" + sFileforName);
}
if (iReqImage1.Width >= iReqImage1.Height)
{
System.Drawing.Bitmap blarge = new System.Drawing.Bitmap((iReqImage1.Width * 260 / iReqImage1.Height), 260);
System.Drawing.Bitmap originalImage3 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics glarge = System.Drawing.Graphics.FromImage(blarge);
try
{
glarge.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
glarge.DrawImage(originalImage3, 0, 0, (iReqImage1.Width * 260 / iReqImage1.Height), 260);
blarge.Save(basePath + "large\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
glarge.Dispose();
blarge.Dispose();
originalImage3.Dispose();
//iReqImage1.Dispose();
}
}
else if (iReqImage1.Height > iReqImage1.Width)
{
System.Drawing.Bitmap blarge = new System.Drawing.Bitmap(310, (iReqImage1.Height * 310 / iReqImage1.Width));
System.Drawing.Bitmap originalImage3 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics glarge = System.Drawing.Graphics.FromImage(blarge);
try
{
glarge.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
glarge.DrawImage(originalImage3, 0, 0, 310, (iReqImage1.Height * 310 / iReqImage1.Width));
blarge.Save(basePath + "large\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
glarge.Dispose();
blarge.Dispose();
originalImage3.Dispose();
//iReqImage1.Dispose();
}
}
if (iReqImage1.Width >= iReqImage1.Height)
{
System.Drawing.Bitmap bsmall = new System.Drawing.Bitmap((iReqImage1.Width * 80 / iReqImage1.Height), 80);
System.Drawing.Bitmap originalImage4 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gsmall = System.Drawing.Graphics.FromImage(bsmall);
try
{
gsmall.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gsmall.DrawImage(originalImage4, 0, 0, (iReqImage1.Width * 80 / iReqImage1.Height), 80);
bsmall.Save(basePath + "small\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gsmall.Dispose();
bsmall.Dispose();
originalImage4.Dispose();
//iReqImage1.Dispose();
}
}
else if (iReqImage1.Height > iReqImage1.Width)
{
System.Drawing.Bitmap bsmall = new System.Drawing.Bitmap(150, (iReqImage1.Height * 150 / iReqImage1.Width));
System.Drawing.Bitmap originalImage4 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gsmall = System.Drawing.Graphics.FromImage(bsmall);
try
{
gsmall.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gsmall.DrawImage(originalImage4, 0, 0, 150, (iReqImage1.Height * 150 / iReqImage1.Width));
bsmall.Save(basePath + "small\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gsmall.Dispose();
bsmall.Dispose();
originalImage4.Dispose();
//iReqImage1.Dispose();
}
}
if (iReqImage1.Width >= 638)
{
System.Drawing.Bitmap bSource = new System.Drawing.Bitmap(638, (iReqImage1.Height * 638 / iReqImage1.Width));
System.Drawing.Bitmap originalImage3 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gSource = System.Drawing.Graphics.FromImage(bSource);
try
{
gSource.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gSource.DrawImage(originalImage3, 0, 0, 638, (iReqImage1.Height * 638 / iReqImage1.Width));
bSource.Save(basePath + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gSource.Dispose();
bSource.Dispose();
originalImage3.Dispose();
iReqImage1.Dispose();
}
}
else
{
iReqImage1.Save(basePath + sFileforName);
}
string sPhotoUrl = ConfigurationManager.AppSettings.Get("photourl");
Response.Write("uploadedfiles/small/" + sFileforName);
}
}
catch (Exception ex)
{

}
}
Private string GetSafeFileName(string fileName, string galleryPath, string sExtn)
{
string sDateTimeFormat = "";
string tmpFileName = "";
string newFileName = "";
try
{
sDateTimeFormat = String.Format("-{0}-{1}-{2}-{3}-{4}-{5}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
tmpFileName = fileName + sDateTimeFormat + sExtn;
newFileName = fileName + sDateTimeFormat + sExtn;
int j = 1;
while (File.Exists(galleryPath + newFileName))
{
newFileName = j + "_" + tmpFileName;
j++;
}
}
catch (Exception ex)
{
LogError("GetSafeFileName", "", ex);
}
return newFileName;
}


Upload Success



This function gets triggered after successive upload and it is as follows

function uploadSuccess(file, serverData) {
try {
var vid = $(file).attr('id');

addImage(serverData,vid);

var progress = new FileProgress(file, this.customSettings.upload_target);

progress.setStatus("Thumbnail Created.");
progress.toggleCancel(false);

} catch (ex) {
this.debug(ex);
}
}
function uploadComplete(file) {
try {
/* I want the next upload to continue automatically so I'll call startUpload here */
if (this.getStats().files_queued > 0) {
this.startUpload();
} else {
var progress = new FileProgress(file, this.customSettings.upload_target);
progress.setComplete();
progress.setStatus("All images received.");
progress.toggleCancel(false);
}
} catch (ex) {
this.debug(ex);
}
}
function addImage(src,vid) {
var newDiv = document.createElement("div");
newDiv.setAttribute("class", "divStyle");
var newImg = document.createElement("img");
newImg.style.margin = "5px";
newImg.id = 'img' + vid;
newDiv.appendChild(newImg);
document.getElementById("thumbnails").appendChild(newDiv);
newImg.onload = function () {
fadeIn(newImg, 0);
};
newImg.src = src;
var divHtml = 'Remove';
$('#img' + vid).after(divHtml);
$('#divProgress').show();
}


In the method addImage a div,img and anchor tags were created which is then binded to the parent div-thumbnails this function in short is used to display the
recently uploaded images.

Remove the uploaded Image



This is done to remove the uploaded images and it is as follows


function deleteImage(vid) {
var imgsrc = $('#img' + vid).attr('src');
var lnkId = $('#img' + vid).attr('id');
var imgId = $('#lnk' + vid).attr('id');

$.ajax({
type: 'Post',
url: 'uploadMultipleImages.aspx?QuStr=removephoto&photosrc=' + imgsrc,
dataType: "json",
data: null,
success: function (data) {
removephotosSucess(data);
}
});
$('#' + lnkId).remove();
$('#' + imgId).remove();
$('#divProgress').hide();
}

after the successful ajax call is made the file gets the following function is referred and it is as follows,

public void RemovePhoto()
{
try
{
//string FileName = Path.GetFileName("uploadedfiles/small/photos-2012-9-6-12-32-17.jpg").ToString();

string sPhotoUrl = ConfigurationManager.AppSettings.Get("photourl");
string sPhotoDir = ConfigurationManager.AppSettings.Get("sharephotosupload");
string imgsrcfull = Request.QueryString["photosrc"];
string imgsrc = "";

//imgsrc = imgsrcfull.Replace(sPhotoUrl + "original/", "");
imgsrc = Path.GetFileName(imgsrcfull).ToString();

string largepath = sPhotoDir + "large\\";
string smallpath = sPhotoDir + "small\\";
string originalpath = sPhotoDir + "original\\";
string thumbnailpath = sPhotoDir + "thumbnail\\";
string thumbnailfullpath = sPhotoDir + "thumbnailfull\\";

string sLfile = largepath + imgsrc;
string sFile = smallpath + imgsrc;
string sRootfile = Server.MapPath("uploadedfiles/" + imgsrc);

string sOfile = originalpath + imgsrc;
string sTnfile = thumbnailpath + imgsrc;
string sTnFfile = thumbnailfullpath + imgsrc;

if (File.Exists(sOfile))
File.Delete(sOfile);
if (File.Exists(sTnfile))
File.Delete(sTnfile);
if (File.Exists(sTnFfile))
File.Delete(sTnFfile);
if (File.Exists(sLfile))
File.Delete(sLfile);
if (File.Exists(sFile))
File.Delete(sFile);
if (File.Exists(sRootfile))
File.Delete(sRootfile);
}
catch (Exception ex)
{
LogError("RemovePhoto", "", ex);
}
}




The entire code behind is as follows:

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string sType = Request["QuStr"];
if (sType == "photos")
{
UploadPhoto();
}
else if (sType == "removephoto")
{
RemovePhoto();
}
}
public void UploadPhoto()
{
string sAlbumname = "";
sAlbumname = "photos";
try
{
string photocount = Request.QueryString["hPhotoTitlecount"];

XmlDocument xDoc = new XmlDocument();
string sFileName = "";

sFileName = Request.Files[0].FileName;

string sImagContentType = Request.Files[0].ContentType.ToLower();

if (sImagContentType.Contains(";"))
sImagContentType = sImagContentType.Substring(0, sImagContentType.IndexOf(';'));

string sImgFName = Request.Files[0].FileName + "";

bool isValidFileName = false;

Regex r = new Regex("^([^\\.]+)+(\\.(?i)(gif|jpeg|pjpeg|jpg|bmp|png))$");
if (r.IsMatch(sImgFName))
isValidFileName = true;

sFileName = sFileName.Replace(",", "-");
System.Drawing.Image iReqImage1 = System.Drawing.Image.FromStream(Request.Files[0].InputStream);

string pathPhotoDir = "";
if (ConfigurationManager.AppSettings.Get("sharephotosupload") != null)
pathPhotoDir = ConfigurationManager.AppSettings.Get("sharephotosupload").ToString();

if (isValidFileName)
{
string sFileExt = sFileName;
string sFileforName = "";
//sFileExt = sFileExt.Substring(sFileExt.LastIndexOf('.'));
sFileExt = ".jpg";

if (sAlbumname.Length > 250)
sAlbumname = sAlbumname.Substring(0, 250);
string file = pathPhotoDir + "original" + sFileName;

sFileforName = GetSafeFileName(sAlbumname, pathPhotoDir, sFileExt);
sFileforName = Regex.Replace(sFileforName, @"\s+", "-");
string basePath = pathPhotoDir;

DirectoryInfo basedir = new DirectoryInfo(pathPhotoDir);
if (basedir.Exists == false)
basedir.Create();

DirectoryInfo originaldir = new DirectoryInfo(basedir + "original");
if (originaldir.Exists == false)
originaldir.Create();

DirectoryInfo thumbnailfull = new DirectoryInfo(basedir + "thumbnailfull");
if (thumbnailfull.Exists == false)
thumbnailfull.Create();

DirectoryInfo thumbnail = new DirectoryInfo(basedir + "thumbnail");
if (thumbnail.Exists == false)
thumbnail.Create();

DirectoryInfo large = new DirectoryInfo(basedir + "large");
if (large.Exists == false)
large.Create();

DirectoryInfo small = new DirectoryInfo(basedir + "small");
if (small.Exists == false)
small.Create();

iReqImage1.Save(basePath + "original\\" + sFileforName);

if (iReqImage1.Width >= iReqImage1.Height)
{

System.Drawing.Bitmap ImgOriginal = new System.Drawing.Bitmap(iReqImage1);

System.Drawing.Bitmap bThumb = new System.Drawing.Bitmap((iReqImage1.Width * 170 / iReqImage1.Height), 170);
System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bThumb);
try
{
gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gThumb.DrawImage(originalImage, 0, 0, (iReqImage1.Width * 170 / iReqImage1.Height), 170);
bThumb.Save(basePath + "thumbnail\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gThumb.Dispose();
bThumb.Dispose();
originalImage.Dispose();
// iReqImage1.Dispose();
}
}
else if (iReqImage1.Height > iReqImage1.Width)
{
System.Drawing.Bitmap ImgOriginal = new System.Drawing.Bitmap(iReqImage1);

System.Drawing.Bitmap bThumb = new System.Drawing.Bitmap(150, (iReqImage1.Height * 150 / iReqImage1.Width));
System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bThumb);
try
{
gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gThumb.DrawImage(originalImage, 0, 0, 150, (iReqImage1.Height * 150 / iReqImage1.Width));
bThumb.Save(basePath + "thumbnail\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gThumb.Dispose();
bThumb.Dispose();
originalImage.Dispose();
// iReqImage1.Dispose();
}
}

if (iReqImage1.Width >= 770)
{
System.Drawing.Bitmap bThumbnailfull = new System.Drawing.Bitmap(600, (iReqImage1.Height * 600 / iReqImage1.Width));
System.Drawing.Bitmap originalImage2 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gThumbnailfull = System.Drawing.Graphics.FromImage(bThumbnailfull);
try
{
gThumbnailfull.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gThumbnailfull.DrawImage(originalImage2, 0, 0, 600, (iReqImage1.Height * 600 / iReqImage1.Width));
bThumbnailfull.Save(basePath + "thumbnailfull\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gThumbnailfull.Dispose();
bThumbnailfull.Dispose();
originalImage2.Dispose();
//iReqImage1.Dispose();
}
}
else
{
iReqImage1.Save(basePath + "thumbnailfull\\" + sFileforName);
}
if (iReqImage1.Width >= iReqImage1.Height)
{
System.Drawing.Bitmap blarge = new System.Drawing.Bitmap((iReqImage1.Width * 260 / iReqImage1.Height), 260);
System.Drawing.Bitmap originalImage3 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics glarge = System.Drawing.Graphics.FromImage(blarge);
try
{
glarge.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
glarge.DrawImage(originalImage3, 0, 0, (iReqImage1.Width * 260 / iReqImage1.Height), 260);
blarge.Save(basePath + "large\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
glarge.Dispose();
blarge.Dispose();
originalImage3.Dispose();
//iReqImage1.Dispose();
}
}
else if (iReqImage1.Height > iReqImage1.Width)
{
System.Drawing.Bitmap blarge = new System.Drawing.Bitmap(310, (iReqImage1.Height * 310 / iReqImage1.Width));
System.Drawing.Bitmap originalImage3 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics glarge = System.Drawing.Graphics.FromImage(blarge);
try
{
glarge.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
glarge.DrawImage(originalImage3, 0, 0, 310, (iReqImage1.Height * 310 / iReqImage1.Width));
blarge.Save(basePath + "large\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
glarge.Dispose();
blarge.Dispose();
originalImage3.Dispose();
//iReqImage1.Dispose();
}
}
if (iReqImage1.Width >= iReqImage1.Height)
{
System.Drawing.Bitmap bsmall = new System.Drawing.Bitmap((iReqImage1.Width * 80 / iReqImage1.Height), 80);
System.Drawing.Bitmap originalImage4 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gsmall = System.Drawing.Graphics.FromImage(bsmall);
try
{
gsmall.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gsmall.DrawImage(originalImage4, 0, 0, (iReqImage1.Width * 80 / iReqImage1.Height), 80);
bsmall.Save(basePath + "small\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gsmall.Dispose();
bsmall.Dispose();
originalImage4.Dispose();
//iReqImage1.Dispose();
}
}
else if (iReqImage1.Height > iReqImage1.Width)
{
System.Drawing.Bitmap bsmall = new System.Drawing.Bitmap(150, (iReqImage1.Height * 150 / iReqImage1.Width));
System.Drawing.Bitmap originalImage4 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gsmall = System.Drawing.Graphics.FromImage(bsmall);
try
{
gsmall.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gsmall.DrawImage(originalImage4, 0, 0, 150, (iReqImage1.Height * 150 / iReqImage1.Width));
bsmall.Save(basePath + "small\\" + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gsmall.Dispose();
bsmall.Dispose();
originalImage4.Dispose();
//iReqImage1.Dispose();
}
}
if (iReqImage1.Width >= 638)
{
System.Drawing.Bitmap bSource = new System.Drawing.Bitmap(638, (iReqImage1.Height * 638 / iReqImage1.Width));
System.Drawing.Bitmap originalImage3 = new System.Drawing.Bitmap(iReqImage1);
System.Drawing.Graphics gSource = System.Drawing.Graphics.FromImage(bSource);
try
{
gSource.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gSource.DrawImage(originalImage3, 0, 0, 638, (iReqImage1.Height * 638 / iReqImage1.Width));
bSource.Save(basePath + sFileforName);
}
catch (Exception ex)
{

}
finally
{
gSource.Dispose();
bSource.Dispose();
originalImage3.Dispose();
iReqImage1.Dispose();
}
}
else
{
iReqImage1.Save(basePath + sFileforName);
}
string sPhotoUrl = ConfigurationManager.AppSettings.Get("photourl");
Response.Write("uploadedfiles/small/" + sFileforName);
}
}
catch (Exception ex)
{
LogError("UploadPhoto", "error occurred while uploading photo", ex);
}
}
private string GetSafeFileName(string fileName, string galleryPath, string sExtn)
{
string sDateTimeFormat = "";
string tmpFileName = "";
string newFileName = "";
try
{
sDateTimeFormat = String.Format("-{0}-{1}-{2}-{3}-{4}-{5}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
tmpFileName = fileName + sDateTimeFormat + sExtn;
newFileName = fileName + sDateTimeFormat + sExtn;
int j = 1;
while (File.Exists(galleryPath + newFileName))
{
newFileName = j + "_" + tmpFileName;
j++;
}
}
catch (Exception ex)
{
LogError("GetSafeFileName", "", ex);
}
return newFileName;
}
public void RemovePhoto()
{
try
{
//string FileName = Path.GetFileName("uploadedfiles/small/photos-2012-9-6-12-32-17.jpg").ToString();

string sPhotoUrl = ConfigurationManager.AppSettings.Get("photourl");
string sPhotoDir = ConfigurationManager.AppSettings.Get("sharephotosupload");
string imgsrcfull = Request.QueryString["photosrc"];
string imgsrc = "";

//imgsrc = imgsrcfull.Replace(sPhotoUrl + "original/", "");
imgsrc = Path.GetFileName(imgsrcfull).ToString();

string largepath = sPhotoDir + "large\\";
string smallpath = sPhotoDir + "small\\";
string originalpath = sPhotoDir + "original\\";
string thumbnailpath = sPhotoDir + "thumbnail\\";
string thumbnailfullpath = sPhotoDir + "thumbnailfull\\";

string sLfile = largepath + imgsrc;
string sFile = smallpath + imgsrc;
string sRootfile = Server.MapPath("uploadedfiles/" + imgsrc);

string sOfile = originalpath + imgsrc;
string sTnfile = thumbnailpath + imgsrc;
string sTnFfile = thumbnailfullpath + imgsrc;

if (File.Exists(sOfile))
File.Delete(sOfile);
if (File.Exists(sTnfile))
File.Delete(sTnfile);
if (File.Exists(sTnFfile))
File.Delete(sTnFfile);
if (File.Exists(sLfile))
File.Delete(sLfile);
if (File.Exists(sFile))
File.Delete(sFile);
if (File.Exists(sRootfile))
File.Delete(sRootfile);
}
catch (Exception ex)
{

}
}

</script>


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: