ho questo upload per caricare resizare e salvare sul server delle miniature..
però quando incappo in file molto pesanti non essendoci un preload o una scritta attendere pare che nulla stia accadendo fino ad operazione ultimata....
e molti chiudono la pagina pensando che nulla stia succedendo
è possibile aggiungere un preload gif oppure un testo ATTENDERE PREGO
durante l'operazione di carcamento e resize?
ecco il codice
Codice PHP:
<%@ Page Language="C#" LCID=1040 Debug="true" Trace="false" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<script Language="C#" runat="server">
void Page_Load(object sender, System.EventArgs e)
{
titletext2.Text = titletext.Text = "";
} // void Page_Load(object sender, System.EventArgs e)
bool IsImage(string fileName)
{
string ext = Path.GetExtension(fileName).ToLower();
bool imageFlag = false;
if (ext != null)
{
switch (ext)
{
case ".emf" : // Enhanced Windows metafile image format
case ".exif" : // Exchangable Image Format
case ".ico" : // Windows icon image format (extension .ico)
case ".wmf" : // Windows metafile image format (extension .wmf)
case ".png" : // Specifies the W3C Portable Network Graphics image format (extension .png)
case ".gif" : // Graphics Interchange Format image format (extension .gif)
case ".bmp" : //Bitmap image format (extension .bmp)
case ".tiff" : // Tag Image File Format (extension .tif)
case ".tif" : // Tag Image File Format (extension .tif)
case ".jpeg" : // Joint Photographic Experts Group image format (extensions .jpg, .jpeg)
case ".jpg" : imageFlag = true; break; // Joint Photographic Experts Group image format (extensions .jpg, .jpeg)
default: imageFlag = false; break; // Not a supported file type.
} // switch (ext)
} // if (ext != null)
else
imageFlag = false;
return imageFlag;
} // bool IsImage(string fileName)
void UploadAndResizeAnImage(HttpPostedFile file, double newWidth, double newHeight, string virPath, bool overwrite)
{
string destPath = ".";
double ThumbnailWidth = 64.0;
double ThumbnailHeight = 64.0;
bool owr = false;
if (virPath != "") destPath = virPath;
if (newWidth > 0) ThumbnailWidth = newWidth;
if (newHeight > 0) ThumbnailHeight = newHeight;
if (overwrite)
owr = true;
else
owr = true;
int pathCheck = (destPath.Substring(destPath.LastIndexOf('/') + 1)).Length;
if (pathCheck > 0) destPath += "/";
string fileName = Request.QueryString["melavivo"] + Path.GetExtension(file.FileName).ToLower();
double orgImgWidth = 0.0;
double orgImgHeight = 0.0;
double rThumbnailWidth = 0.0;
double rThumbnailHeight = 0.0;
destPath = Server.MapPath(destPath);
if ( (!File.Exists(destPath + fileName)) && (fileName.Length != 0) || (owr))
{
if (!IsImage(fileName))
{
if (fileName.Length != 0)
Status.Text += "<span style=\"color:Red;\">File '" + fileName + "' Formato immagine non valido. Caricamento Annullato.</span>
";
return;
} // if (!IsImage(fileName))
using(System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream))
{
rThumbnailWidth = ThumbnailWidth;
rThumbnailHeight = ThumbnailHeight;
orgImgWidth = image.Width;
orgImgHeight = image.Height;
// Se l'immagine è più piccola del Thumbnail forziamo le dimensioni di
// quest'ultimo alle dimensioni dell'immagine.
if (orgImgWidth < rThumbnailWidth) rThumbnailWidth = image.Width;
if (orgImgHeight < rThumbnailHeight) rThumbnailHeight = image.Height;
// Se le dimensioni dell'immagine e del Thumbnail corrispondono non facciamo nulla.
// quindi se l'immagine è quadrata...
if (image.Width != image.Height)
{
// Se l'immagine non è quadrata
// continuiamo a fare i nostri controlli per calcolarne le
// giuste dimensioni e quindi creare la nostra nuova immagine con
// l'aspectratio corretto.
// Se l'immagine è più larga che alta
if (image.Width > image.Height)
{
// L > H => L = tL
// La larghezza del TN sarà quella da noi stabilita
// quindi dobbiamo solo assegnarla.
rThumbnailWidth = rThumbnailWidth;
// L'altezza del TN invece dovrà essere ricalcolata
// in proporzione alla larghezza dell'immagine originale.
// L > H => tH = (H * tL)/ L
rThumbnailHeight = (double)((image.Height * rThumbnailWidth) / image.Width);
} // if (tmpImage.Width > tmpImage.Height)
else
{
// Altrimenti se l'immagine è più alta
if (image.Width < image.Height)
{
// L < H => H = tH
// L'altezza del TN sarà quella da noi stabilita
// quindi dobbiamo solo assegnarla.
rThumbnailHeight = rThumbnailHeight;
// La larghezza del TN invece dovrà essere ricalcolata
// in proporzione alla altezza dell'immagine originale.
// L < H => tL = (L * tH)/ H
rThumbnailWidth = (double)((image.Width * rThumbnailHeight) / image.Height);
} // if (image.Width < image.Height)
} // else
} // if (image.Width != image.Height)
using(Bitmap bitmap = new Bitmap(image, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
{
string message = "Uploaded";
if (File.Exists(destPath + fileName) && (owr)) message = "OverWritted";
bitmap.Save(destPath + fileName, image.RawFormat);
Status.Text += "<span style=\"color:Green;\">Foto inserita con successo!!!
</span>
";
} // using(Bitmap bitmap = new Bitmap(image, (int)(rThumbnailWidth), (int)(rThumbnailHeight)))
} // using(System.Drawing.Image image = System.Drawing.Image.FromFile(file))
} // if ( (!File.Exists(destPath + fileName)) && (fileName != "") || (owr))
else
{
if (File.Exists(destPath + fileName) && (!owr))
Status.Text += "<span style=\"color:Red;\">Error : The file \"<u>[b][i]" + fileName + "[/i][/b]</u>\" already exist... Upload Aborted.</span>
";
}
} // void UploadImage(HttpPostedFile file)
protected void SubmitButton_Click(Object sender, EventArgs e)
{
HttpFileCollection uploadedFiles = Request.Files;
Status.Text = "";
for (int i = 0; i < uploadedFiles.Count; i ++)
{
// Qui sotto definisci le dimensioni che vuoi, la cartella di destinazione
// e se l'applicazione deve sovrascrivere o no i files preesistenti.
//
// UploadAndResizeAnImage(HttpPostedFile file, double newWidth, double newHeight, string virPath, bool overwrite)
//
// Quindi in se scrivo come qui sotto :
UploadAndResizeAnImage(uploadedFiles[0], 350.0, 350.0, "./public/", false);
// significa che voglio che ogni immagine inviata (uploadedFiles[i])
// venga ridimensionata a 640x480 (tenendo conto però delle proporzioni originali)
// nella cartella "./public/" (che dovrà avere i permessi di scrittura)
// e se il file esiste già non deve essere sovrascritto (overwrite = false).
// Chiaro? Più di così ;)
}
} // protected void SubmitButton_Click(Object sender, EventArgs e)
</script>
grazie infinite