gw经销商上传部分代码

protected void btnSave_Click(object sender, EventArgs e)

    {

        string filePath = "";

        if (FileUpload1.HasFile)

        {

            string fileType = System.IO.Path.GetExtension(FileUpload1.FileName).ToString();

            CN_T_Sysdictionary dicModel = CN_T_SysdictionaryDal.cn_t_SysdictionaryDal.GetModel(" key='经销商在线附件类型'");

            string defaultFileType = ".rar|.zip|.doc|.docx";//默认格式

            if (dicModel != null)

            {

                defaultFileType = dicModel.VALUE;

            }



            if (dicModel.VALUE.Contains(fileType))

            {

                int fileLenth = FileUpload1.PostedFile.ContentLength;

                CN_T_Sysdictionary dicSizeModel = CN_T_SysdictionaryDal.cn_t_SysdictionaryDal.GetModel(" dicname='经销商在线附件大小'");

                int dicSize = 2 * 1024 * 1024;//默认文件大小

                if (dicSizeModel != null)

                {

                    dicSize = Convert.ToInt32(dicSizeModel.VALUE) * 1024 * 1024;

                }

                if (fileLenth <= dicSize)

                {

                    //edit by jarry 2012-11-4 增加随机文件名

                    int index = FileUpload1.FileName.LastIndexOf(".");

                    string lastName = FileUpload1.FileName.Substring(index, FileUpload1.FileName.Length - index);//文件后缀

                    string newname = DateTime.Now.ToString("yyyyMMddhhmmss") + lastName;//新文件名

                    filePath = "../uploads/" + newname;

                }

                else

                {

                    ScriptHelper.Alert("上传文件大小不能超过:" + dicSizeModel.VALUE + "M");

                    return;

                }

            }

            else

            {

                ScriptHelper.Alert("上传文件的类型只能为" + dicModel.VALUE);

                return;

            }

        }



        string userName = txtUserName.Text.Replace("#", "");

        string unit = txtUnit.Text.Replace("#", "");

        string address = txtAddress.Text.Replace("#", "");

        string email = txtEmail.Text.Replace("#", "");

        string qq = txtQQ.Text;

        string phone = txtPhone.Text;

        string content = txtContent.Text.Replace("#", "");



        StringBuilder msgContent = new StringBuilder();

        msgContent.Append(userName);

        msgContent.Append("#");

        msgContent.Append(unit);

        msgContent.Append("#");

        msgContent.Append(address);

        msgContent.Append("#");

        msgContent.Append(email);

        msgContent.Append("#");

        msgContent.Append(qq);

        msgContent.Append("#");

        msgContent.Append(phone);

        msgContent.Append("#");

        msgContent.Append(content);



        CN_T_UserMsg model = new CN_T_UserMsg();

        model.BYDOMAIN = "官网";

        model.TITLE = "经销商在线问答";

        model.MSGCONTENT = msgContent.ToString();

        model.ISSHOW = 0;

        model.TYPE = "经销商在线问答";

        model.FILEPATH = filePath;

        try

        {

            CN_T_UserMsgDAL.cN_T_UserMsgDAL.Add(model);

            if (FileUpload1.HasFile)

            {

                FileUpload1.SaveAs(Server.MapPath(filePath));

            }

            InputClearText();// 清空所有文本

            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('提交成功!')", true);

        }

        catch

        {

            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('提交失败!')", true);

        }

    }

你可能感兴趣的:(代码)