Exemplo n.º 1
0
        private string GetFilePath(string path, string fileName = "")
        {
            var server = HttpContext.Current.Server;

            ZFiles.CreateDirectory(server.MapPath(path));
            return(server.MapPath(path + fileName));
        }
        public string PostFileData(string id, string tName, string tId, int type)
        {
            if (tId != null && tId != "")
            {
                tId = tId.Remove(tId.Length - 1, 1);
                string sql = string.Format(@"
delete QMS_QualityReportDoc where ID in({0})", tId);
                using (var db = Db.Context("Mms"))
                {
                    db.Sql(sql).Execute();
                }
            }
            string info = string.Empty;

            try
            {
                //获取客户端上传的文件集合
                HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                //判断是否存在文件
                Random rd = new Random();
                if (files.Count > 0)
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        //获取文件集合中的第一个文件(每次只上传一个文件)
                        HttpPostedFile file = files[i];
                        if (file.FileName == "")
                        {
                            continue;
                        }
                        //定义文件存放的目标路径
                        string targetDir = HttpContext.Current.Server.MapPath("~/Upload/" + new HomeApiController().GetPath(tName));
                        //创建目标路径
                        ZFiles.CreateDirectory(targetDir);
                        //组合成文件的完整路径
                        var    r           = rd.Next(9999, 99999);
                        string newFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + r + "." + file.FileName.Substring(file.FileName.LastIndexOf('.') + 1, file.FileName.Length - file.FileName.LastIndexOf('.') - 1);
                        string path        = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(newFileName));
                        //保存上传的文件到指定路径中
                        try
                        {
                            file.SaveAs(path);
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                        info = "上传成功";

                        //string filePath = HttpContext.Current.Server.MapPath("~/Upload/" + path) + newFileName;
                        string InsertSql = string.Format(@"insert into QMS_QualityReportDoc values('',{5},'{0}','{1}',1,'{2}',getdate(),'{2}',getdate(),'{3}','{4}')", newFileName, path, MmsHelper.GetUserName(), file.FileName, id, type);
                        using (var db = Db.Context("Mms"))
                        {
                            db.Sql(InsertSql).Execute();
                        }
                    }
                }
                else
                {
                    info = "上传失败";
                }
            }
            catch
            {
                info = "上传失败";
            }
            return(info);
        }