Uploadfileファイルアップロードコントロールの使用方法
4360 ワード
まずPage_Loadメソッドクリックイベントの追加
画像の長さと幅を変える方法は以下の通りです.
後で勉強して見るためだけに使う.
protected void Page_Load(object sender, EventArgs e)
{
btnUploadFile2.ServerClick += new EventHandler(btnUploadFile2_ServerClick);
}
メソッド実装イベントでよく使用されるいくつかの書き方を追加する void btnUploadFile2_ServerClick(object sender, EventArgs e)
{
try
{
if (TxtMedCode2.Text.Length == 0)
{
Page.RegisterStartupScript("msg", "alert(' !') ");
return;
}
//1.
bool fileOK = false;
string path = Server.MapPath("~/upload/");
if (this.FileUpload2.HasFile)
{
string fileException = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
string[] allowedException = { ".jpg", ".gif", ".bmp", ".ico", ".png" };
for (int i = 0; i < allowedException.Length; i++)
{
if (fileException == allowedException[i])
fileOK = true;
}
}
if (fileOK)
{
try
{
DateTime dt1 = DateTime.Now;
// upload
string strFileName = this.TxtMedCode2.Text.ToString() + dt1.ToString("yyyyMMddHHmmss");
this.FileUpload2.SaveAs(path + strFileName + "_Init" + ".jpg");
// , image
System.Drawing.Image img_Big = this.PhotoSizeChange(path + strFileName + "_Init" + ".jpg", 300, 165);
// upload
img_Big.Save(path + strFileName + "_Commendation" + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
//
LblImgUrl2.Text = "upload/" + strFileName + "_Commendation" + ".jpg";
//
if (Mn_PostId(TxtMedCode2.Text) == "0")
{
//LblDeatil1.Text = " , !";
}
else
{
LblPostUrl2.Text = Mn_PostId(TxtMedCode2.Text);
}
//Img1.ImageUrl = LblImgUrl1.Text.ToString();
//
img_Big.Dispose();
//this.LblDeatil1.Text = " !";
}
catch (Exception ex)
{
Response.Redirect("~/Error.aspx?error=" + ex.Message.Replace("
", "").Replace("
", ""));
Page.RegisterStartupScript("msg", "alert(' , !') ");
}
}
else
{
Page.RegisterStartupScript("msg", "alert(' !') ");
}
}
catch (Exception e2)
{
Response.Redirect("~/Error.aspx?error=" + e2.Message.Replace("
", "").Replace("
", ""));
}
}
画像の長さと幅を変える方法は以下の通りです.
private System.Drawing.Image PhotoSizeChange(string strPhoto, int iWidth, int iHeight)
{
//*****strPhoto ******//
//
System.Drawing.Image image = new Bitmap(strPhoto);//
//
System.Drawing.Image newImage = image.GetThumbnailImage(iWidth, iHeight, null, new IntPtr());
Graphics g = Graphics.FromImage(newImage);
//
g.DrawImage(newImage, iHeight, iHeight, newImage.Width, newImage.Height);
g.Dispose();
return newImage;
}
後で勉強して見るためだけに使う.