最近做的项目跟asp.net上传文件内容有关,故将代码贴出,以便网友查阅,提供解决此类问题思路:如出现任何不理解问题,请留言,及时帮您解决!
<%@ page language="c#" autoeventwireup="true" codefile="login.aspx.cs" inherits="login" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>::::上传管理系统::::</title>
</head>
<body>
<form id="form1" runat="server">
<div >
<br />
<asp:label id="labeltitle" runat="server" font-bold="true" font-size="xx-large" forecolor="black"
text="上传管理"></asp:label><br />
<br />
<asp:fileupload id="fileupload1" runat="server" width="369px"/>
<asp:button id="buttonup" runat="server" height="21px" text="上 传" width="67px" onclick="buttonup_click" /><br />
<br />
<asp:label id="labeltitle2" runat="server" forecolor="red" text="*上传文件不能超过2m"></asp:label><br />
<b>原文件名:</b><span id ="filename" runat="server" /><br />
<b>上传到服务器:</b><span id="savedir" runat="server" /><br />
<b>缩略图地址:</b><span id ="ssavedir" runat ="server" /><br />
<b>文件类型:</b><span id ="filetype" runat ="server" /><br />
<b>文件大小:</b><span id="filelength" runat ="server" /><br />
<b>文件拓展名:</b><span id="fileextention" runat="server" /><br />
<b>上传日期:<br /><span id ="updatetime" runat ="server" /><br />
图片预览:<br />
<asp:image id="image1" runat="server" imageurl="~/login.aspx" bordercolor="#0000c0"/><br />
</b>
<br />
<br />
</div>
</form>
</body>
</html>
********************************************************
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using mwo.model.info;
using mwo.dal.info;
public partial class login : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void buttonup_click(object sender, eventargs e)
{
if (fileupload1.postedfile.filename != null)
{
try
{
filename.innerhtml = fileupload1.postedfile.filename;//getphotoname
updatetime.innerhtml = datetime.now.toshortdatestring();//updatetime
#region setphotosize
filelength.innerhtml = countsize(fileupload1.postedfile.contentlength);
if (fileupload1.postedfile.contentlength > 1024 * 1024 * 2)
{
response.write("<script>alert('图片不能超过规定大小!');</script>");
}
else
{
#region setphotoformat
filetype.innerhtml = fileupload1.postedfile.contenttype;
fileextention.innerhtml = system.io.path.getextension(fileupload1.postedfile.filename).toupper();
string m_filename = "www.VeVb.com-" + datetime.now.tostring("yyyy-mm-dd-hh-mm-ss-ffff").replace(".", "-") + fileextention.innerhtml;
string m_sfilename = "www.VeVb.com-" + datetime.now.tostring("yyyy-mm-dd-hh-mm-ss-ffff").replace(".", "-") + fileextention.innerhtml;
if (fileextention.innerhtml == ".jpg")
{
string m_savepath = server.mappath("./photo/") + m_filename;
savedir.innerhtml = m_savepath;
#region setsmallphoto
this.makesmallimg(fileupload1.postedfile, this.server.mappath("./sphoto/") + m_sfilename, 118, 90);
string m_ssavepath = this.server.mappath("./sphoto/") + m_sfilename;
ssavedir.innerhtml = m_ssavepath;
#endregion
fileupload1.postedfile.saveas(m_savepath);
response.write("<script>alert('图片文件保存成功!');</script>");
#region savedatabasee
photoinfo m_pinfo = new photoinfo();
m_pinfo.organization = "5173";
m_pinfo.picname = filename.innerhtml;
m_pinfo.servername = "glsdb";
m_pinfo.cdate = datetime.now;
m_pinfo.exp = fileextention.innerhtml;
m_pinfo.flag = 0;
m_pinfo.gamenickname = "bruce";//textbox控件内容
m_pinfo.moonstar = 0;
m_pinfo.showindex = 0;
m_pinfo.spichttpaddr = m_ssavepath;
m_pinfo.type = 0;
m_pinfo.vote = 0;
m_pinfo.pichttpaddr=m_savepath;
photodao m_pdao = new photodao();
m_pdao.insert(m_pinfo);
#endregion
}
else
{
response.write("<script>alert('图片格式不正确,请选择图片文件!');</script>");
}
#endregion
}
#endregion
}
catch (exception m_ex)
{
response .write ("<script>alert('"+m_ex.tostring ()+"');</script>");
}
}
else if (fileupload1.postedfile.filename == "")
{
response.write("<script>alert('上传文件不能为空!');</script>");
}
}
#region
/// <summary>
/// 计算文件大小函数,size为字节大小
/// </summary>
/// <param name="size">初始文件大小</param>
/// <returns></returns>
public string countsize(long size)
{
string m_strsize = "";
long factsize = 0; factsize = size;
if (factsize <= 1024)
m_strsize = factsize.tostring() + "byte";
if(factsize>=1024&&factsize<=1048576)
m_strsize=(factsize/1024).tostring ()+"k";
if (factsize >= 1048576 && factsize <= 10485760)
m_strsize = (factsize / 1024 / 1024).tostring() + "m";
return m_strsize;
}
#endregion
#region savesmallphoto
/// <summary>
/// 高清晰缩略图算法
/// </summary>
/// <param name="postfile">图片文件对象</param>
/// <param name="saveimg">要保存为缩略图的源文件</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public void makesmallimg(system.web.httppostedfile postfile, string saveimg, system.double width, system.double height)
{
//sourcephotoname
string m_originalfilename = postfile.filename;
string m_strgoodfile = saveimg;
//getphotoobject from sourcefile
system.drawing.image m_image = system.drawing.image.fromstream(postfile.inputstream, true);
system.double newwidth, newheight;
if (m_image.width > m_image.height)
{
newwidth = width;
newheight = m_image.height * (newwidth / m_image.width);
}
else
{
newheight = height;
newwidth = (newheight / m_image.height) * m_image.width;
}
if (newwidth > width)
{
newwidth = width;
}
if (newheight > height)
{
newheight = height;
}
//getphotosize
system.drawing.size size =new system.drawing.size((int)newwidth, (int)newheight);
//the new of bimp photo
system.drawing.image bitmap = new system.drawing.bitmap(size.width, size.height);
// the new of palette
system.drawing.graphics g = system.drawing.graphics.fromimage(bitmap);
// set hightquality arithmetic for graphics
g.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
//设置高质量,低速度呈现平滑程度
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
//clearcanvas
g.clear(system.drawing.color.white);
//在指定位置画图
g.drawimage(m_image, new system.drawing.rectangle(0, 0, bitmap.width, bitmap.height),
new system.drawing.rectangle(0, 0, m_image.width, m_image.height),
system.drawing.graphicsunit.pixel);
//savephoto of hightfocus
bitmap.save(m_strgoodfile, system.drawing.imaging.imageformat.jpeg);
//disposeres
g.dispose();
m_image.dispose();
bitmap.dispose();
}
#endregion
}
新闻热点
疑难解答
图片精选