Hejsan!
Jag försöker fixa så man kan ladda upp flera filer samtidigt. Det fungerar felfritt på min dator men sen när jag laddar upp det till mitt webhotell så laddas bara den första bilden upp. De andra laddas inte upp men finaly körs.
Denna kod körs när man ska ladda upp:
if ((ListBox1.Items.Count != 0))
{
foreach (System.Web.HttpPostedFile HIF in hif)
{
try
{
if (!Directory.Exists(Server.MapPath("Upload/" + Convert.ToInt32(Session["intID"]) + "/Blog/" + DateTime.Now.ToString("yyyy-MM-dd"))))
{
Directory.CreateDirectory(Server.MapPath("Upload/" + Convert.ToInt32(Session["intID"]) + "/Blog/" + DateTime.Now.ToString("yyyy-MM-dd")));
}
if (stringcut(HIF.ContentType) != "image/")
{
lblMessage.Text = "* " + HIF.FileName.ToString() + " är ej en bild !";
return;
}
else if (HIF.ContentLength > 500000)
{
lblMessage.Text = "* " + HIF.FileName.ToString() + " är för stor!(" + HIF.ContentLength.ToString() + " byte) Max är 500000 byte";
return;
}
try
{
Image imgPhoto = null;
imgPhoto = FixedSize(System.Drawing.Image.FromStream(HIF.InputStream), 357, 320);
imgPhoto.Save(Server.MapPath("Upload/" + Convert.ToInt32(Session["intID"]) + "/Blog/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + DateTime.Now.ToString("ddMMyyhhmmss") + "-" + HIF.ContentLength.ToString() + ".jpg"), ImageFormat.Jpeg);
imgPhoto.Dispose();
}
finally
{
hif.Remove(HIF.InputStream);
ListBox1.Items.Remove(HIF.FileName);
lblMessage.Text = "";
using (IDbCommand objCommand2 = new MySqlCommand())
{
objCommand2.CommandText = "INSERT INTO tblBlogImages (intUserID, strImageURL, dtmBlogDate) VALUES (?in_UserID, ?in_ImageURL, ?in_Date)";
objCommand2.Parameters.Add(new MySqlParameter("in_UserID", Convert.ToInt32(Session["intID"])));
objCommand2.Parameters.Add(new MySqlParameter("in_ImageURL", Convert.ToString("Upload/" + Convert.ToInt32(Session["intID"]) + "/Blog/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + DateTime.Now.ToString("ddMMyyhhmmss") + "-" + HIF.ContentLength.ToString() + ".jpg")));
objCommand2.Parameters.Add(new MySqlParameter("in_Date", DateTime.Now.ToString("yyyy-MM-dd")));
int ExecuteNonQuery2 = new DataAccess().ExecuteNoResult(ConfigurationManager.AppSettings["DatabaseConnection"], objCommand2);
}
}
}
catch (Exception err)
{
lblMessage.Text = err.ToString();
}
}
hif.Clear();
ListBox1.Items.Clear();
}
FixedSize:
static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
//if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((Height - (sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
string stringcut(string strArgument)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(strArgument);
return (sb.Remove(6, (sb.Length - 6)).ToString());
}
}
Är det något fel på koden eller är det några rättigheter som ska fixas till på webbhotellet?
Tack på förhand!
/ Timmie
EDIT, ni kan ta bort tråden, det var något fel med DateTime.Now("yyyy-MM-dd"); bytte ut det mot ett hiddenfield som redan hade det värdet.