亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Asp.net上傳文件至目錄

標簽:
產品

Asp.net上传文件,把文件存入至目录中。限制上传文件大小,限制上传文件格式。存取得原来文件名,创建新的文件名。

把文件临时存入临时目录中,处理完毕,最后再移至真正目录。

参考代码:

复制代码 //上传时,判断文件是否大于限定大小
        if (this.FileUpload1.PostedFile.ContentLength > 104857600) //单位KB
        {
            objInsusJsUtility.JsAlert("You select the file larger than 100MB");
            return;
        }

        //创建一个临时文件夹
        string tempPath = "~/Temp/";
        //判断目录是否存在
        if (!Directory.Exists(Server.MapPath(tempPath)))
        {
            //如果不存在,创建它
            Directory.CreateDirectory(Server.MapPath(tempPath));
        }

       //取得上传文件
        string uploadfile = FileUpload1.PostedFile.FileName;
        //取得原文件名,存入数据库中,这样在aspx显示回原来的文件名
        string oldFileName = uploadfile.Substring(uploadfile.LastIndexOf(@"\") + 1);
        //取得文件的扩展名
        string fileExtension = uploadfile.Substring(uploadfile.LastIndexOf("."));
        //产生新文件名
        string newFileName = objGuid.ToString() + fileExtension;

        //建立存储的目录
        string directory = Mediadirectory + this.ddlMediaType.SelectedItem.Value + "/";
        
        //判断目录是否存在
        if (!Directory.Exists(Server.MapPath(directory)))
        {
           //如果不存在,创建它
            Directory.CreateDirectory(Server.MapPath(directory));
        }

        //新文件
        string newFile = Server.MapPath(tempPath + newFileName);
        
        //保存文件(暂存入一个临时文件夹中)
        FileUpload1.SaveAs(newFile);

        //限定上传的文件类型
        string[] fileClass = { "7076", "4838" };  //7076 is FLV;4838 is wmv;
        if (!InsusBase.CompareFileClass(newFile, fileClass))
        {
            objInsusJsUtility.JsAlert("You did not specify a media file.The file format is wmv,flv");
            return;
        }

        try
        {
           //存入数据库中
            objMedia.Insert(this.ddlMediaType.SelectedItem.Value, this.txtSubject.Text.Trim(), this.txtDescription.Text.Trim(), directory, oldFileName, newFileName);
            //把文件从临时文件夹中,移至真正的目录。
            File.Move(newFile, Server.MapPath(directory + newFileName));
            objInsusJsUtility.JsAlert("视频上传成功。", "this", "Media.aspx");
        }
        catch (Exception ex)
        {
            //抛出异常
            InsusBase.InsusException(ex);
        }复制代码

 

Web.config配置可上传大文件,asp.net默认情况之下只能上传4MB,另外一点就是,maxRequestLength单位是MB。

 <system.web>      
        <httpRuntime maxRequestLength="102400" useFullyQualifiedRedirectUrl="true" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true"/>
    </system.web>

 

 

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消