.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

CodeToday: Upload Image Options · View
ball
Posted: Monday, January 14, 2008 12:54:56 PM

Rank: อาจารย์
Groups: Administration

Joined: 12/1/2007
Posts: 473
Location: Bangkok

CodeToday: Upload Image and Resize

using System.IO;
using System.Web;
 
public static string UploadImage(string newFileName, string directory, FileUpload fileUpload, int imageSize, bool overwrite)
{
    if (fileUpload.HasFile)
    {
        string oldFileName = fileUpload.FileName;
        DirectoryInfo dirTempInfo = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + "Temp");
        if (!dirTempInfo.Exists) dirTempInfo.Create();
        string tempPath = dirTempInfo.FullName + @"\" + oldFileName;
        fileUpload.SaveAs(tempPath);
        FileInfo tempInfo = new FileInfo(tempPath);
        string extension = tempInfo.Extension;
        if (newFileName == "") newFileName = tempInfo.Name.Substring(0, tempInfo.Name.IndexOf(extension));
        string newFullName = newFileName + extension;
        DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + directory);
        if (!dirInfo.Exists) dirInfo.Create();
        string filePath = dirInfo.FullName + @"\" + newFullName;
        FileInfo fileInfo = new FileInfo(filePath);
        if (!fileInfo.Exists || overwrite)
        {
            if (fileInfo.Exists) fileInfo.Delete();
            if (imageSize > 0)
            {
                ResizeImage(tempPath, filePath, imageSize);
                tempInfo.Delete();
            }
            else tempInfo.MoveTo(filePath);
        }
        else tempInfo.Delete();
        return directory + "/" + newFullName;
    }
    else return "";
}

กำหนดขนาดไฟล์สูงสุดที่ upload หน่วยเป็น KB ได้ที่ web.config

<system.web>
    <httpRuntime maxRequestLength="4096"/>
</system.web>

ResizeImage ได้มาจาก CodeToday: ResizeImage



Imagination is more Important than Knowledge... /A.Einstein
diji
Posted: Tuesday, January 22, 2008 9:16:23 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 1/14/2008
Posts: 6
Location: phet

การupload หลายๆๆรูปพร้อมกัน เหมือนที่เราupload file ใน hotmail ทำไงค่ะ

ball
Posted: Tuesday, January 22, 2008 9:49:37 AM

Rank: อาจารย์
Groups: Administration

Joined: 12/1/2007
Posts: 473
Location: Bangkok

โค๊ดที่ผมให้เป็น Static Method อยู่แล้ว คุณเอาไปแปะไว้ที่ คลาสไหนก็ได้

แล้วก็เรียกใช้งานผ่านคลาสนั้นกี่ครั้งก็ได้



Imagination is more Important than Knowledge... /A.Einstein
Barbie
Posted: Thursday, June 12, 2008 2:27:07 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 6/3/2008
Posts: 3
Location: BKK

ในตัวอย่างการใช้ฟังก์ชัน UploadImage()
ก็ให้ใส่ค่าตามที่ระบุไว้ได้เลยเหรอคะ
แล้วถ้า ชื่อไฟล์มาจาก Fileupload ต้องทำยังไง

iceeu
Posted: Sunday, October 19, 2008 11:13:43 PM

Rank: มือสมัครเล่น
Groups: Member

Joined: 4/12/2008
Posts: 15

แล้วเรียกใช้ยังไงอ่ะ

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by