上传文件

 

<p class="pb10">
    <asp:TextBox ID="txtDownloadUrl" runat="server" Width="300px" Enabled="false"></asp:TextBox>
</p>
<asp:FileUpload ID="fupDownloadFiles" runat="server" /> 
<asp:Button ID="btnUploadDownload" runat="server" CssClass="btnBg" Text="确认上传" onclick="btnUploadDownload_Click" OnClientClick="return checkType()"/>

 

<script type="text/javascript">  

    function checkType() {

        //得到上传文件的值
        var fileName = document.getElementById("<%=fupDownloadFiles.ClientID%>").value;

        if (fileName == "") {
            alert("请选择要上传的文件!");
            return false;
        }

        //返回String对象中子字符串最后出现的位置.  
        var seat = fileName.lastIndexOf(".");

        //返回位于String对象中指定位置的子字符串并转换为小写.  
        var extension = fileName.substring(seat).toLowerCase();

        //判断允许上传的文件格式 ".jpg", ".jpeg", ".gif", ".png"
        var allowed = [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt", ".zip", ".rar", ".gz", ".bz2", ".pdf"];
        for (var i = 0; i < allowed.length; i++) {
            if (!(allowed[i] != extension)) {
                return true;
            }
        }
        alert("不支持" + extension + "格式");
        return false;
    }  
    
</script>

 

    //上传Download文件
    protected void btnUploadDownload_Click(object sender, EventArgs e)
    {
        if (fupDownloadFiles.PostedFile.FileName != "")
        {

            //定义上传路径(在当前目录下的uploadfile文件下)
            string uploadpath = this.Server.MapPath("~/ckeditor/userfiles/Download");
            //取得文件名
            string tmpfilename = fupDownloadFiles.PostedFile.FileName;
            //文件名
            string filename = tmpfilename.Substring(tmpfilename.LastIndexOf("\\") + 1);
            //求取后缀名
            string suffix = filename.Substring(filename.LastIndexOf(".")).ToLower();

            bool flg = false;
            string[] allowed = { ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt", ".zip", ".rar", ".gz", ".bz2", ".pdf" };
            for (var i = 0; i < allowed.Length; i++)
            {
                if (allowed[i] != suffix)
                {
                    continue;
                }
                else
                {
                    flg = true;
                    break;
                }
            }
            if (!flg)
            {
                JScript.Alert("不支持“" + suffix + "”格式", this);
                return;
            }

            //重命名:DateTime
            Random ro = new Random();
            filename = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssff"), ro.Next(1000, 9999), suffix);

            //原文件的保存路径
            string fileSavePath = uploadpath + "\\" + filename;

            txtDownloadUrl.Text = filename;

            //保存文件           
            fupDownloadFiles.SaveAs(fileSavePath);
        }
        else
        {
            JScript.Alert("请选择要上传的文件", this);
            return;
        }
    }

 

posted @ 2015-10-27 17:07  linyongqin  阅读(115)  评论(0编辑  收藏  举报