如何用ASP.NET实现图片上传?(80分)

km168 2003-10-26 12:42:56
如题
...全文
299 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
szj820 2003-12-24
  • 打赏
  • 举报
回复
up
starry11 2003-11-04
  • 打赏
  • 举报
回复
mark
lveight 2003-11-03
  • 打赏
  • 举报
回复
mark
活靶子哥哥 2003-10-26
  • 打赏
  • 举报
回复
http://www.aspxboy.com/ShowArticle.Aspx?ID=218
hukejie 2003-10-26
  • 打赏
  • 举报
回复
mark
whl9234 2003-10-26
  • 打赏
  • 举报
回复
我昨天做了一天,上传多少图片都可以了
我用了HtmlInputFile控件,
简单的说upload.PostedFile.SaveAs(upurl);(upload是HtmlInputFile控件)
你还可以加一些验证上传文件大小,类型等方法,都可以在upload.PostedFile.中找到
我是把文件夹名存到数据库中的,所以可以上传多张,
zsww 2003-10-26
  • 打赏
  • 举报
回复
学习!!!

-------努力学习 不断实践 虚心讨教--------
youngby 2003-10-26
  • 打赏
  • 举报
回复
<%@ 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>
rgbcn 2003-10-26
  • 打赏
  • 举报
回复
asp.net上传图片并同时生成缩略图


http://www.pconline.com.cn/pcedu/empolder/net/10309/215678.html
qiuji 2003-10-26
  • 打赏
  • 举报
回复
图片上传,给你一个例子:
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();
}
}

}
sjzwinfor 2003-10-26
  • 打赏
  • 举报
回复
CSDN - 文档中心 - .net

标题 用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>


steveson 2003-10-26
  • 打赏
  • 举报
回复
很好啊
ceocio 2003-10-26
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/read_article.asp?id=20937
cnhgj 2003-10-26
  • 打赏
  • 举报
回复
http://www.codeproject.com/aspnet/fileupload.asp?target=UPload
Abac 2003-10-26
  • 打赏
  • 举报
回复
楼上的很详细了,可以直接用,Up一下
gOODiDEA 2003-10-26
  • 打赏
  • 举报
回复
上传(upload)文件的codebehind代码
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();

}


62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧