最近做的項目跟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 = "m.survivalescaperooms.com-" + datetime.now.tostring("yyyy-mm-dd-hh-mm-ss-ffff").replace(".", "-") + fileextention.innerhtml;
string m_sfilename = "m.survivalescaperooms.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
}
新聞熱點
疑難解答
圖片精選