ASP.NET之上传文件管理策略

分类: asp.net技巧   出处:iocblog整理  更新时间:2008-12-04   添加到收藏  

最近做的项目跟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 style="border-right: #ffff00 thin dashed; border-top: #ffff00 thin dashed; margin-bottom: 4px; margin-left: 55px; border-left: #ffff00 thin dashed; line-height: normal; margin-right: 55px; padding-top: 1px; border-bottom: #ffff00 thin dashed; letter-spacing: normal; position: static; background-color: silver; text-align: center">
        <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"/>
        &nbsp;<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 />
        &nbsp;</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.mwo.com-" + datetime.now.tostring("yyyy-mm-dd-hh-mm-ss-ffff").replace(".", "-") + fileextention.innerhtml;
                     string m_sfilename = "www.mwo.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控件内容(www.iocblog.net 文章来源)
                         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>");
        }
    }

[1] [2] 下一页


Tag: 上传



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。