Torna al Thread
<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void UploadBtn_Click(Object sender, EventArgs e)
{
String UploadedFile = MyFile.PostedFile.FileName;
int ExtractPos = UploadedFile.LastIndexOf("\\") + 1;
//to retrieve only Filename from the complete path
String UploadedFileName = UploadedFile.Substring(ExtractPos,UploadedFile.Length - ExtractPos);
// Display information about posted file. Div is invisible by default
FileName.InnerHtml =UploadedFileName;
MyContentType.InnerHtml = MyFile.PostedFile.ContentType;
ContentLength.InnerHtml = MyFile.PostedFile.ContentLength.ToString();
FileDetails.Visible = true; //div is made visible
// Save uploaded file to server at the in the Pics folder
String percorso= Request.PhysicalPath;
int daEliminare= percorso.Length -31;
string nuovo = percorso.Substring(0, daEliminare) + "public/immobili/";
//Response.Write(nuovo);
// Response.End();
//MyFile.PostedFile.SaveAs(Request.PhysicalApplicationPath + "pics\\" + UploadedFileName );
//MyFile.PostedFile.SaveAs("C:/Inetpub/wwwroot/public/prod/" + UploadedFileName );
MyFile.PostedFile.SaveAs(nuovo + UploadedFileName);
//thumbnail creation starts
try
{
//Read in the image filename whose thumbnail has to be created
String imageUrl= UploadedFileName;
String idProduct= Request.QueryString["id"];
String numero = Request.Form["numero"];
//Read in the width and height
//int imageHeight =Convert.ToInt32(h.Text);
//int imageWidth = Convert.ToInt32(w.Text);
//You may even specify a standard thumbnail size
int imageWidth = 80;
int imageHeight = 100;
if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0 )
{
//We found a / or \
Response.End();
}
//the uploaded image will be stored in the Pics folder.
//to get resize the image, the original image has to be accessed from the
//Pics folder
imageUrl = nuovo + imageUrl;
//System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
int altezzaNuova;
int larghezzaNuova;
// altezzaNuova = fullSizeImg.Height * imageWidth / fullSizeImg.Width;
larghezzaNuova = fullSizeImg.Width * imageHeight / fullSizeImg.Height;
//nuovaDim.Text = Convert.ToString(larghezzaNuova);
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
//System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero);
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(larghezzaNuova, imageHeight, dummyCallBack, IntPtr.Zero);
//We need to create a unique filename for each generated image
DateTime MyDate = DateTime.Now;
string nuovoFileName;
string[] words = UploadedFileName.Split('.');
//String MyString = "min_" + MyDate.ToString("ddMMyyhhmmss") + ".png" ;
String MyString = "min_" + words[0] + ".jpg";
//Save the thumbnail in Png format. You may change it to a diff format with the ImageFormat property
// thumbNailImg.Save ( Request.PhysicalApplicationPath + "pics\\" + MyString , ImageFormat.Jpeg);
thumbNailImg.Save(nuovo + MyString, ImageFormat.Jpeg);
thumbNailImg.Dispose();
//Display the original & the newly generated thumbnail
// Image1.AlternateText = "Original image";
//Image1.ImageUrl = nuovo + UploadedFileName;
// Image2.AlternateText = "Thumbnail";
// Image2.ImageUrl = nuovo + MyString;
OleDbConnection conn = null;
OleDbCommand cmd = null;
OleDbDataReader dr = null;
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" + Server.MapPath("/mdb-database/prova.mdb"));
conn.Open();
System.Console.WriteLine("Connessione aperta sul db");
string sql = "update TblImmobili set imageUrl"+numero+"='"+ UploadedFileName +"', smallImageUrl"+numero+"='" + MyString + "' where idNews= "+ idProduct ;
System.Console.WriteLine(sql);
cmd = new OleDbCommand(sql, conn);
System.Console.WriteLine("Stringa sql eseguita");
cmd.ExecuteNonQuery();
System.Console.WriteLine("Scrivo nella tabella");
conn.Close();
}
catch(Exception ex)
{
Response.Write("An error occurred - " + ex.ToString());
}
}
//this function is reqd for thumbnail creation
public bool ThumbnailCallback()
{
return false;
}
</script>
<html>
<head>
<title>BackOffice</title>
<link href="../stileAdmin.css" rel="stylesheet" type="text/css">
</head>
<body rightmargin="0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td height="100px" valign="top" >
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td><img src="images/spacer.gif" width="20" height="1"></td>
<td>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td></td>
<td width="100%"></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2" class="carea">
<form action="PNGUploader.aspx" method="post" enctype="multipart/form-data" runat="server">
<h4>Salva immagine </h4>
<span class="linkMenu">Selezione un file File: </span>
<input id="MyFile" type="file" runat="server" />
<br>
<br>
<span class="linkMenu">Immagine n°</span>
<select name="numero" id="numero">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br>
<br/>
<input type="submit" value="Upload!" runat="server" onserverclick="UploadBtn_Click" />
<br/>
<br/>
<hr/>
<div id="FileDetails" runat="server" visible="false">Nome File: <span id="FileName" runat="server"></span><br/>
ContentType: <span id="MyContentType" runat="server"></span><br/>
ContentLength: <span id="ContentLength" runat="server">bytes
<br />
</span>
<br> <font color="#009900"> <b> Immagine inserita correttamente</b></font> </div>
</form>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
</table>
</td>
<td width="100%"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="images/spacer.gif" height="13" width="1"></td>
</tr>
<tr>
<td> </td>
</tr>
</table>