<%@ Import Namespace="System.IO" %> <html> <head> <script language="VB" runat="server"> 'UploadButton1 上传按纽 'UploadFile1 上传文件名 'SaveasFile1 另存文件名 'SavePath1 上传绝对路径 'Error1 提示的错误信息 Sub UploadButton1_Click(sender As Object, e As EventArgs) Dim SavePath1 As String="C:\" If UploadFile1.Value = "" Then Error1.InnerHtml = "错误:您必须选择一个文件!" Return End If If SaveasFile1.Value = "" Then Error1.InnerHtml = "错误:必须输入一个文件名!" Return End If If Not IsNothing(UploadFile1.PostedFile) Then Dim filepath As String = Path.Combine(SavePath1, Path.GetFileName(SaveasFile1.Value)) Try UploadFile1.PostedFile.SaveAs(filepath) Error1.InnerHtml = "文件成功上载到服务器上的 " & filepath Catch Exc As Exception Error1.InnerHtml = "文件上载出错<b>" & filepath & "</b><br>" & Exc.ToString() End Try End If End Sub </script> </head> <body> <form enctype="multipart/form-data" runat="server"> 选择上载文件:<input id="UploadFile1" type=file runat="server"><br> 另存为文件名:<input id="SaveasFile1" type="text" runat="server">(需后缀)<br> <input type=button id="UploadButton1" value="上传" OnServerClick="UploadButton1_Click" runat="server"><br> <div id="Error1" style="color:red" runat="server" /> </form></body> </html>
eKiller 发表于 1/27/2002 11:38:47 AM ASP.NET ←返回版面 [快速返回] 功能:
1。把图片文件(JPG GIF PNG)上传,
2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
3。并自动生成指定宽度的(在web.config中设置宽度)
4。和指定格式的(在web.config中指定缩略图的格式)
5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
6。可以判断是否已经存在文件
7。如果不覆盖,则给出错误
8。如果选中"覆盖原图"checkbox,则覆盖原图。
9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
11。缩略图的文件名尺寸。
12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。--------------------
public void UploadFile(object sender, System.EventArgs e)
{
string imgNameOnly, imgNameNoExt, imgExt;
string imgThumbnail;
int erroNumber = 0;
System.Drawing.Image oriImg, newImg;
string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString();
string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower();
int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]);
string fileExt; StringBuilder picInfo = new StringBuilder();
if(Page.IsValid)
{
for(int i = 0;i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();
imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
{ if(System.IO.File.Exists(strFePicSavePath + imgNameOnly) && (checkboxlistRewrite.Items[i].Selected == false))
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br>" );
}
}
else
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br>" );
} } if(erroNumber > 0)
{
picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br>");
}
else
{
for(int i = 0;i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName);
imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();
oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
newImg = oriImg.GetThumbnailImage(intFeThumbWidth, intFeThumbWidth * oriImg.Height/oriImg.Width,null,new System.IntPtr(0));
switch(imgExt)
{
//case ".jpeg":
case ".jpg":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png);
break;
}
//oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
switch(strFePicThumbFormat)
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case "jpg":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;
case "gif":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif);
imgThumbnail = imgNameOnly + "_thumb.gif";
break;
case "png":
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png);
imgThumbnail = imgNameOnly + "_thumb.png";
break;
default:
newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;
}//switch
picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br>");
picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br><br>");
oriImg.Dispose();
newImg.Dispose();
}//for
picInfo.Append("<font color=red>所有操作成功</font><br>"); }// if erronumber = 0
}
else
{
picInfo.Append("<font color=red>有错误,请检查。操作未成功</font><br>"); } lblPicInfo.Text = picInfo.ToString(); }
标题 用ASP.NET上传图片并生成可带版权信息的缩略图 xiahouwen(原作)
关键字 缩略图,上传,
<%@ Page Language="C#" ResponseEncoding="gb2312" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<script runat="server"> void Page_Load(Object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
ImgPreview.Visible=false;
}
}
void GetThumbnailImage(int width,int height,string strInfo,int left,int right)
{
string file="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1);
string newfile="Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1)+".jpg";
string strAdd=strInfo;
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
System.Drawing.Image thumbnailImage =
oldimage.GetThumbnailImage(width, height,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
Response.Clear();
Bitmap output=new Bitmap(thumbnailImage);
Graphics g=Graphics.FromImage(output);
g.DrawString(strAdd,new Font("Courier New", 14),new SolidBrush(Color.Red),left,right);
output.Save(Server.MapPath(newfile),System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/gif";
ImgPreview.Visible=true;
ImgPreview.ImageUrl=newfile;
}
bool ThumbnailCallback()
{
return true;
}
void Button_Click(object sender, EventArgs e)
{
int width,height,left,right;
string strAddInfo=txtAddInfo.Text;
width=Int32.Parse(txtWidth.Text);
height=Int32.Parse(txtHeight.Text);
left=Int32.Parse(txtLeft.Text);
right=Int32.Parse(txtRight.Text);
if(!(uploadFile.PostedFile.ContentLength>0))
{
lblErrInfo.Text="没有选择文件";
}
else
{
string path = Server.MapPath("./Uploads/"+uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('\\')+1));
if(File.Exists(path))
{
lblErrInfo.Text="已经有同名文件";
}
else
{
uploadFile.PostedFile.SaveAs(path);
GetThumbnailImage(width,height,strAddInfo,left,right);
}
}
}
</script>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data" runat="server">
<p>
<input id="uploadFile" type="file" runat="server" />
<asp:Label id="lblErrInfo" runat="server" forecolor="Red"></asp:Label>
</p>
<p>
width:<asp:TextBox id="txtWidth" runat="server" Width="40px">100</asp:TextBox>
height:<asp:TextBox id="txtHeight" runat="server" Width="40px">150</asp:TextBox>
</p>
<p>
添加信息:<asp:TextBox id="txtAddInfo" runat="server"> AspxBoy.Com</asp:TextBox>
</p>
<p>
信息位置:left:<asp:TextBox id="txtLeft" runat="server" Width="40px">10</asp:TextBox>
right:<asp:TextBox id="txtRight" runat="server" Width="40px">135</asp:TextBox>
</p>
<p>
<input id="button" type="button" value="上传生成所略图" onServerClick="Button_Click" runat="server" />
</p>
<p><asp:Image id="ImgPreview" runat="server"></asp:Image>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
1.在.aspx文件中:(添加enctype="multipart/form-data")
<form id="upload_img_common" method="post" runat="server" enctype="multipart/form-data"><span id="Message" runat="server"></span><INPUT id="File1" type="file" name="File1" runat="server">2.在.aspx.cs中:
using System.IO;
protected System.Web.UI.HtmlControls.HtmlGenericControl Message;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;private void btnOK_Click(object sender, System.EventArgs e)
{
string filepath=Server.MapPath("img/"+Path.GetFileName(File1.PostedFile.FileName));
if(File.Exists(filepath))
{
Message.InnerHtml="上传文件重名,请改名后再上传!";
Message.Style["Color"]="red";
return;
}
else
{
if(File1.PostedFile!=null)
try
{
File1.PostedFile.SaveAs(filepath);
Message.InnerHtml="<b>成功上传!</b>";
}
catch (Exception exc)
{
Message.InnerHtml="保存文件时出错<b>" + filepath + "</b><br>"+ exc.ToString();
}
} }
http://www.pconline.com.cn/pcedu/empolder/net/10309/215678.html
<html>
<head>
<script language="VB" runat="server"> 'UploadButton1 上传按纽
'UploadFile1 上传文件名
'SaveasFile1 另存文件名
'SavePath1 上传绝对路径
'Error1 提示的错误信息 Sub UploadButton1_Click(sender As Object, e As EventArgs) Dim SavePath1 As String="C:\" If UploadFile1.Value = "" Then
Error1.InnerHtml = "错误:您必须选择一个文件!"
Return
End If If SaveasFile1.Value = "" Then
Error1.InnerHtml = "错误:必须输入一个文件名!"
Return
End If If Not IsNothing(UploadFile1.PostedFile) Then
Dim filepath As String = Path.Combine(SavePath1, Path.GetFileName(SaveasFile1.Value))
Try
UploadFile1.PostedFile.SaveAs(filepath)
Error1.InnerHtml = "文件成功上载到服务器上的 " & filepath
Catch Exc As Exception
Error1.InnerHtml = "文件上载出错<b>" & filepath & "</b><br>" & Exc.ToString()
End Try
End If
End Sub
</script>
</head>
<body> <form enctype="multipart/form-data" runat="server">
选择上载文件:<input id="UploadFile1" type=file runat="server"><br>
另存为文件名:<input id="SaveasFile1" type="text" runat="server">(需后缀)<br>
<input type=button id="UploadButton1" value="上传" OnServerClick="UploadButton1_Click" runat="server"><br>
<div id="Error1" style="color:red" runat="server" />
</form></body>
</html>
我用了HtmlInputFile控件,
简单的说upload.PostedFile.SaveAs(upurl);(upload是HtmlInputFile控件)
你还可以加一些验证上传文件大小,类型等方法,都可以在upload.PostedFile.中找到
我是把文件夹名存到数据库中的,所以可以上传多张,