How to upload images!

Source: Internet
Author: User

First:

Use the upload tool included in 2005 to upload:

Public static string uploadimage (system. Web. UI. webcontrols. fileupload filebox, string savepath)
{
Datetime dattime = system. datetime. now;
String filename = filebox. filename; // file name

String extension = ""; // File Extension

String strtemp = "";
String relativepath = ""; // relative path
String filepath = ""; // physical path

If (filebox. hasfile)
{
Extension = path. getextension (filename );
Strtemp = dattime. tow.datestring (). 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 ('the file exceeds 50 kb and cannot be uploaded! ') </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 ('uploaded successfully') </SCRIPT> ");
Return relativepath;
}
Else
{
// System. Web. httpcontext. Current. response. Write ("<script language = JavaScript> window. Alert ('upload failed') </SCRIPT> ");
Return relativepath;
}

}

 

This is another method:

This method is used to upload images in freetextbox: (refer to uploading images)

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; // Jin Ru 2005.5.11
If (width! = 0) & (height! = 0 ))
{
K = convert. todouble (width/height );

If (k> = 1)
{
If (width >=750)
{
Length = 750;
Height = width/K;
}
}
Else
{
If (Height & gt; = 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;
}
}

The width and height values are used to return values and display the size in the editing box.

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 ();}

(First, width and height are respectively paid to this. form. elements ['IP']. value, this. form. elements ['aw']. value, this. form. elements ['ih ']. value and then "OK" to the receiving page)

<Input id = "enter" onclick = "returnimage (this. form. elements ['IP']. value, this. form. elements ['aw']. value, this. form. elements ['ih ']. value );"
Type = "button" value = "OK">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.