Пример #1
0
    public string Uploads()
    {
        HttpFileCollection files = Request.Files;
        AjaxErrorResult    r     = new AjaxErrorResult("error");

        try
        {
            if (files == null)
            {
                return(r.ToString());
            }

            if (files.Count == 0)
            {
                return(r.ToString());
            }

            for (int i = 0; i < files.Count; i++)
            {
                HttpPostedFile f               = files[i];
                string         ex              = f.FileName.Substring(f.FileName.LastIndexOf(".") + 1);
                string         str_path        = "/file/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
                string         str_file_name   = Guid.NewGuid().ToString("N") + "." + ex;
                string         str_path_absult = Server.MapPath("~" + str_path);
                if (!System.IO.Directory.Exists(str_path_absult))
                {
                    System.IO.Directory.CreateDirectory(str_path_absult);
                }

                string str_file_url = str_path_absult + str_file_name;

                f.SaveAs(str_file_url);

                return(new AjaxSuccessResult(str_file_url).ToString());
            }

            return(r.ToString());
        }
        catch (Exception ex)
        {
            comm_fun.WriteLog(ex.ToString());
            return(r.ToString());
        }
    }
Пример #2
0
    /// <summary>
    /// 上传一个文件
    /// </summary>
    public void Upload()
    {
        HttpFileCollection files = Request.Files;
        AjaxErrorResult    r     = new AjaxErrorResult("error");

        string str_FD_ID = Request["FD_ID"];
        int    FD_ID     = 0;

        if (!string.IsNullOrEmpty(str_FD_ID))
        {
            FD_ID = Convert.ToInt32(str_FD_ID);
        }
        string BUSINESS_TYPE = Request["BUSINESS_TYPE"];

        if (string.IsNullOrEmpty(BUSINESS_TYPE))
        {
            BUSINESS_TYPE = string.Empty;
        }

        try
        {
            if (files == null)
            {
                Response.Write(r.ToString());
                return;
            }

            if (files.Count == 0)
            {
                Response.Write(r.ToString());
                return;
            }

            HttpPostedFile f             = files[0];
            string         extension     = f.FileName.Substring(f.FileName.LastIndexOf(".") + 1);
            string         str_path_v    = "/file/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            string         str_file_name = Guid.NewGuid().ToString("N") + "." + extension;
            string         str_path_s    = Server.MapPath("~" + str_path_v);
            //string str_file_old = "";
            //Dim i_index As Int32 = str_file_url_v.LastIndexOf("/")
            //Dim str_file_name As String = comm_fun.convert_string(str_file_url_v.Substring(i_index + 1, str_file_url_v.Length - i_index - 1))

            if (!System.IO.Directory.Exists(str_path_s))
            {
                System.IO.Directory.CreateDirectory(str_path_s);
            }
            string str_file_url_s = str_path_s + str_file_name;
            f.SaveAs(str_file_url_s);
            string str_file_url_v = str_path_v + str_file_name;

            //新增文件表
            SYS_FILE f1 = new SYS_FILE();
            f1.FL_ACTIVE        = true;
            f1.FL_SIZE          = f.ContentLength;
            f1.FL_EXTENSION     = extension;
            f1.FL_NAME          = get_old_filename(f.FileName);
            f1.FL_URL           = str_file_url_v;
            f1.FL_CREATE_DATE   = DateTime.Now;
            f1.FL_FD_ID         = FD_ID;
            f1.FL_BUSINESS_TYPE = BUSINESS_TYPE;

            db.SYS_FILE.Add(f1);
            db.SaveChanges();
            Response.Write(new AjaxSuccessResult(f1.FL_ID.ToString()).ToString());
            return;
        }
        catch (Exception ex)
        {
            comm_fun.WriteLog(ex.ToString());
            Response.Write(r.ToString());
            return;
        }
    }