圖片上傳的方法!

來源:互聯網
上載者:User

第一種:

通過2005裡面帶的UPLOAD工具來上傳:

 public static String UploadImage(System.Web.UI.WebControls.FileUpload FileBox, string SavePath)
    {
        DateTime datTime = System.DateTime.Now;
        string FileName = FileBox.FileName;     //檔案名稱

        string Extension = "";  //副檔名

        string strTemp = "";
        string relativePath = "";   //相對路徑
        string FilePath = "";   //實體路徑

        if (FileBox.HasFile)
        {
            Extension = Path.GetExtension(FileName);
            strTemp = datTime.ToShortDateString().Replace("-", "");
            FileName = datTime.ToLongTimeString().Replace(":", "") + datTime.Millisecond + (new Random()).Next(100000)+Extension;
            FileName = strTemp + FileName;
            int FileSize = FileBox.PostedFile.ContentLength;
            if (FileSize > 50000)
            {
                System.Web.HttpContext.Current.Response.Write("<script language=javascript>window.alert('檔案超過50K,無法上傳!')</script>");
                return relativePath;
            }
            relativePath = "~/UploadFiles/" + SavePath + "/"+strTemp+"/";
            FilePath = System.Web.HttpContext.Current.Server.MapPath(relativePath);
            CreateDirectory(FilePath);
            FilePath += FileName;
            relativePath = "/UploadFiles/" + SavePath + "/" + strTemp + "/";
            relativePath += FileName;
            FileBox.SaveAs(FilePath);
            //System.Web.HttpContext.Current.Response.Write("<script language=javascript>window.alert('上傳成功')</script>");
            return relativePath;
        }
        else
        {
            //System.Web.HttpContext.Current.Response.Write("<script language=javascript>window.alert('上傳失敗')</script>");
            return relativePath;
        }

    }  

 

這個是另外一種方法:

此方法用於在FREETEXTBOX中的上傳圖片:(參照上傳圖片)

if (UploadFile.PostedFile.FileName.Trim() != "")
{
if (IsValidFileType(UploadFile.PostedFile.FileName))
{
try
{
string UploadFileName = "";
string UploadFileDestination = "";
string filename= "";
int ppp = 0;
        
filename=(DateTime.Now.ToString()).Replace(":","");
filename= filename.Replace(" ","");
filename= filename.Replace("-","");
    
UploadFileName = UploadFile.PostedFile.FileName;
ppp=UploadFileName.LastIndexOf(".");
UploadFileName=UploadFileName.Substring(ppp,UploadFileName.Length-ppp);
filename = filename+UploadFileName;        
        
        
UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
UploadFileDestination += CurrentImagesFolder.Value;
UploadFileDestination += "//";
UploadFile.PostedFile.SaveAs(UploadFileDestination + filename); 
        
string urrl = Server.MapPath("../images/") + filename;
double width=0;
double height=0;
System.Drawing.Bitmap  imgObj = new Bitmap(urrl);
width=imgObj.Width;        
height=imgObj.Height;
        
double k;//金儒 2005.5.11
if ((width != 0) && (height != 0))
{          
k=Convert.ToDouble( width/height); 
          
if(k>=1)
{
if (width>=750)
{
width=750;
height=width/k;
}
}
else
{
if (height>=750)
{
height=750;
width=k*height;
}
}
}
        
string imgurl = @"../AspNetForums/images/" + filename;
this.iw.Value = width.ToString();
this.ih.Value = height.ToString();
this.ip.Value = imgurl;           
ResultsMessage.Text = UploadSuccessMessage;
}
catch
{
ResultsMessage.Text = UploadFailureMessage;
}
}
else
{
ResultsMessage.Text = InvalidFileTypeMessage;
}
}

其中的width ,height 用於傳回值,用來在編輯框裡顯示大小。

function returnImage(imagename,width,height) { var imgAr = new Array(); imgAr["filename"] = imagename; imgAr["width"] = width; imgAr["height"] = height; window.parent.returnValue = imgAr; window.parent.close(); }

(首先width ,height分別付給this.form.elements['ip'].value,this.form.elements['iw'].value,this.form.elements['ih'].value  然後“確定”傳到接收的頁面)

<INPUT id="enter" onclick="returnImage(this.form.elements['ip'].value,this.form.elements['iw'].value,this.form.elements['ih'].value);"
           type="button" value=" 確定 ">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.