1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Design;
using System.IO;
public partial class UploadImages : System.Web.UI.Page
{
Random random = new Random(); //variable aléatoire pour nommer du FolderName
protected void Page_Load(object sender, EventArgs e)
{
//string ApplicationPath = Request.PhysicalApplicationPath;
//ApplicationPath = ApplicationPath.Substring(0, ApplicationPath.Length - 3);
//lblMessage.Text = ApplicationPath ;
//lblMessage.Text = "";
}
public int InsertedImageID = 0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection uploads = HttpContext.Current.Request.Files;
string ApplicationPath = Request.PhysicalApplicationPath;
ApplicationPath = ApplicationPath.Substring(0, ApplicationPath.Length - 4);
//FolderName du livre selectionné
DALivre dalivre = new DALivre();
string FolderName = dalivre.getFolderNameByIDLivre(Convert.ToInt32(ddlLivre.SelectedItem.Value));
int NombreImages = dalivre.getNombreImagesByIDLivre(Convert.ToInt32(ddlLivre.SelectedItem.Value));
string path = ApplicationPath + "\\images\\";
path = path + FolderName;
//Upload photos
int UploadedImages = 0;
for (int i = 0; i < uploads.Count; i++)
{
HttpPostedFile upload = uploads[i];
if (upload.ContentLength == 0) // si aucune image dans le input en cours alors passer au suivant
continue;
string FileName = System.IO.Path.GetFileName(upload.FileName); // We don't need the path, just the name.
string FileExtention = System.IO.Path.GetExtension(upload.FileName);
if (FileExtention.ToLower() != ".jpg")
{
lblMessage.Text = "Fichiers jpg uniquement.";
btnSubmit.Enabled = true;
return;
}
upload.SaveAs(@ApplicationPath + "\\images\\" + FolderName + "\\page" + (NombreImages + i).ToString() + ".jpg");
UploadedImages++;
lblMessage.Text = "Upload terminé.";
}
NombreImages = NombreImages + UploadedImages;
if (uploads.Count > 0) //update livre, enregistrer NombreImages
{
dalivre.UpdateNombreImages(Convert.ToInt32(ddlLivre.SelectedItem.Value), NombreImages);
}
}
catch (Exception Exp)
{
lblMessage.Text = "Echec lors de l'upload : <br />" + Exp.ToString();
//lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
private void New()
{
//txtFolderName.Enabled = true;
//txtFolderName.Text = "";
btnSubmit.Enabled = false;
}
protected void ddlCategorie_SelectedIndexChanged(object sender, EventArgs e)
{
ddlLivre.Items.Clear();
ddlLivre.Items.Add(new ListItem("Sélectionnez un livre", "0"));
ddlLivre.DataBind();
}
protected void cmdSave_Click(object sender, EventArgs e)
{
try
{
// (1) Read Uploaded file (here Image) in Byte Array
if (File1.PostedFile != null)
{
if (File1.PostedFile.ContentLength > 0)
{
// Get Posted File.
HttpPostedFile objHttpPostedFile = File1.PostedFile;
// Check valid Image type. Create this function according to your need
string FileExtention = System.IO.Path.GetExtension(File1.PostedFile.FileName);
if (FileExtention == ".jpg")
{
// Find its length and convert it to byte array
int intContentlength = objHttpPostedFile.ContentLength;
// Create Byte Array
Byte[] bytImage = new Byte[intContentlength];
// Read Uploaded file in Byte Array
objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength);
//(2) Insert image in database
DAImage daimage = new DAImage();
InsertedImageID = daimage.InsertImage(bytImage, Convert.ToInt32(ddlLivre.SelectedItem.Value));
}
else
{
lblMessage.Text = "Fichiers jpg uniquement.";
//lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
}
}
catch (Exception Exp)
{
lblMessage.Text = "Echec lors de l'enregistrement : <br />" + Exp.ToString();
//lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
protected void cmdRetour_Click(object sender, EventArgs e)
{
Response.Redirect("Ajout.aspx");
}
} |
Partager