求asp.net文件(主要是图片)上传的源码

chenghaicys 2008-12-14 06:02:00
单个文件上传就可以。要能用的。google很久都没看见什么好用的。好多都有错误。
麻烦高手。我的email:86893297@qq.com
...全文
138 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
空心兜兜 2008-12-14
  • 打赏
  • 举报
回复
学习下
robert126 2008-12-14
  • 打赏
  • 举报
回复
路过
wuyq11 2008-12-14
  • 打赏
  • 举报
回复
参考
http://www.cnblogs.com/hutie1980/archive/2008/08/25/1275792.html
http://www.cnblogs.com/ltt1987/archive/2008/04/01/423242.html
http://www.cnblogs.com/jkobtr/archive/2006/06/01/415312.html
octtiger 2008-12-14
  • 打赏
  • 举报
回复
学习中。。。。
zilong4460072 2008-12-14
  • 打赏
  • 举报
回复
关注
jiang_jiajia10 2008-12-14
  • 打赏
  • 举报
回复
http://www.51aspx.com/CV/ImageUpload/
下载这个就可以了。就不给你发email了
dayizhixiaotutu 2008-12-14
  • 打赏
  • 举报
回复
学习
pdsnet 2008-12-14
  • 打赏
  • 举报
回复
 if (FileUpload1.HasFile)
{


string strExPrentFile = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1).ToUpper();
string[] NoExPrentFile = new string[] { "JPG", "BMP", "PNG", "JPEG", "GIF" };
bool IsUp = true;

for (int j = 0; j < NoExPrentFile.Length; j++)
{
if (strExPrentFile.Equals(NoExPrentFile[j]))
{
IsUp = false;

}
}
if (!IsUp)
{

string name = string.Format("{0}.{1}", DateTime.Now.ToString("yyMMddHHmmssfff"), strExPrentFile);
string fileName = name; //文件名称
string sFileName = "x_" + name; //缩略图文件名


string filePath = Server.MapPath("~/UpFile/big/" + fileName); //服务器端文件路径
string sFilePath = Server.MapPath("~/UpFile/small/" + sFileName);//缩略图文件路径


try
{
FileUpload1.SaveAs(filePath);

MakeThumNail(filePath, sFilePath, 130, 130, "HW");
string big = "~/UpFile/big/" + fileName;
string small = "~/UpFile/small/" + sFileName;
Response.Write("<script>parent.document.form1.imgUrl.value='" +big+ "|';</script>");
Response.Write("<script>parent.document.form1.imgUrl1.value='" +small + "|';</script>");
}
catch (Exception ex)
{


Response.Write("<script>alert('文件上传失败,请查看文件是否小于4M')</script>");
}

}
else
{
Response.Write("<script>alert('文件类型出错')</script>");
}


}

 public static void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, string model)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int thumWidth = width; //缩略图的宽度
int thumHeight = height; //缩略图的高度

int x = 0;
int y = 0;

int originalWidth = originalImage.Width; //原始图片的宽度
int originalHeight = originalImage.Height; //原始图片的高度

switch (model)
{
case "HW": //指定高宽缩放,可能变形
break;
case "W": //指定宽度,高度按照比例缩放
thumHeight = originalImage.Height * width / originalImage.Width;
break;
case "H": //指定高度,宽度按照等比例缩放
thumWidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)thumWidth / (double)thumHeight)
{
originalHeight = originalImage.Height;
originalWidth = originalImage.Height * thumWidth / thumHeight;
y = 0;
x = (originalImage.Width - originalWidth) / 2;
}
else
{
originalWidth = originalImage.Width;
originalHeight = originalWidth * height / thumWidth;
x = 0;
y = (originalImage.Height - originalHeight) / 2;
}
break;
default:
break;
}

//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(thumWidth, thumHeight);

//新建一个画板
System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量查值法
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
graphic.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
graphic.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, thumWidth, thumHeight), new System.Drawing.Rectangle(x, y, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);

try
{
bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{

throw ex;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
graphic.Dispose();
}

}
路人乙e 2008-12-14
  • 打赏
  • 举报
回复
string filename = file1.PostFile.FileName;
filename = filename.Substring(filename.LastIndexOf("\\")+1);
file1.PostFile.SaveAs(Server.MapPath("/upload/"+filename));

62,269

社区成员

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

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

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

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