国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET2.0 上傳圖片并為用戶創建相應文件夾

2024-07-10 13:09:32
字體:
來源:轉載
供稿:網友

//前臺代碼

<%@ page language="c#" autoeventwireup="true" codefile="test.aspx.cs" inherits="test" %>
<!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>
       <asp:fileupload id="fileupload1" runat="server" cssclass="input3" width="290px"/>&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:button  id="button1" runat="server" text="上傳圖片并創建文件夾" onclick="button1_click" />
    </div>
    </form>
</body>
</html>

//后臺代碼

using system;
using system.data;
using system.configuration;
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 system.io;
public partial class test : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {

    }
    protected void button1_click(object sender, eventargs e)
    {
        string uploadfilename, uploadfilelastname;
        uploadfilename = fileupload1.postedfile.filename;//文件在本地的原始位置   
        uploadfilelastname = uploadfilename.substring(uploadfilename.lastindexof(".") + 1);//得到文件的擴展名
        random rd = new random();//產生隨機數
        int valationno = 10 + rd.next(99);//產生隨機數
        string suiji = valationno.tostring();//產生隨機數
        string uploadfiletime = datetime.now.tostring("yyyymmddhhmmss") + suiji;//得到系統時間并加上隨機數以便生成上傳圖片名稱
        string newuploadfilename = uploadfiletime + "." + uploadfilelastname;//產生上傳圖片的名稱

        string userdirectory = "testfile";//所要創建文件夾的名字,實際運用中可為用戶注冊id
        string userpath = server.mappath("uploadfiles").tostring() + "http://" + userdirectory + "http://" + "productsimages";
        if (!directory.exists(userpath)) //如果文件夾不存在則創建
        {
            directory.createdirectory(userpath);
        }
        fileupload1.postedfile.saveas(newuploadpahtname);//用保存的方法將圖片上載
        string suser_productsimageurl = "uploadfiles/" + userdirectory + "/" + "productsimages/" + newuploadfilename;//得到服務端圖片的虛擬路徑
        //response.write(suser_productsimageurl);
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闽侯县| 建瓯市| 宁化县| 新邵县| 金山区| 左权县| 乐至县| 富阳市| 商都县| 凤凰县| 利辛县| 精河县| 景洪市| 红安县| 大港区| 定襄县| 临澧县| 安阳县| 江阴市| 临夏市| 台中县| 九江市| 礼泉县| 垣曲县| 横山县| 新乡市| 礼泉县| 四子王旗| 江达县| 如皋市| 桓仁| 镇远县| 恩施市| 林州市| 上栗县| 南昌市| 巴楚县| 夏邑县| 泊头市| 定边县| 喀喇|