Esempio n. 1
0
        /// <summary>
        /// 用户上传文件
        /// </summary>
        /// <param name="context">上下文</param>
        private void SaveAsFile(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset     = "utf-8";
            try
            {
                HttpPostedFile postedFile = context.Request.Files["Filedata"];
                if (postedFile != null)
                {
                    string configPath     = ConfigManager.UploadPath;                                                 //获取配置的文件保存虚拟路径
                    string fileExt        = GetFileExt(postedFile.FileName);                                          //文件扩展名,不含“.”
                    long   fileSize       = postedFile.ContentLength;                                                 //获得文件大小,以字节为单位
                    string fileName       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名
                    string randomFileName = String.Format("{0}.{1}", StringHelper.GetPrimaryKey(), fileExt);          //获取文件新名称,随机数

                    AttachInfo attach = new AttachInfo();

                    //检查文件扩展名是否合法
                    if (!CheckFileExt(fileExt))
                    {
                        attach.code = ReturnCode.Error.ToInt();
                        attach.msg  = "文件格式不合法!";
                        context.Response.Write(JsonHelper.JSSerialize(attach));
                        return;
                    }
                    string virPath = GetUpLoadPath(configPath);                  //相对路径
                    string phyPath = GetMapPath(string.Format("~{0}", virPath)); //绝对路径

                    //检查上传的物理路径是否存在,不存在则创建
                    if (!Directory.Exists(phyPath))
                    {
                        Directory.CreateDirectory(phyPath);
                    }

                    //检查是否为图片
                    if (CheckImage(fileExt))
                    {
                        //如果当前文件大小大于1M,则进行文件压缩
                        if (fileSize >= (Math.Pow(1024, 2)))
                        {
                            byte[] imges = Thumbnail.CompressionImage(postedFile.InputStream, 16L);
                            File.WriteAllBytes(Path.Combine(phyPath, randomFileName), imges);
                        }
                        else
                        {
                            postedFile.SaveAs(string.Format("{0}{1}", phyPath, randomFileName));//保存文件
                        }
                    }
                    else
                    {
                        postedFile.SaveAs(string.Format("{0}{1}", phyPath, randomFileName));//保存文件
                    }
                    string attachPath = string.Format("{0}{1}", virPath, randomFileName);

                    attach.code      = ReturnCode.Succ.ToInt();
                    attach.msg       = "上传成功!";
                    attach.path      = attachPath;
                    attach.name      = fileName;
                    attach.size      = fileSize;
                    attach.extension = fileExt;

                    context.Response.Write(JsonHelper.SerializeObject(attach));
                }
                else
                {
                    context.Response.Write("0");
                }
            }
            catch (Exception ex)
            {
                AttachInfo attach = new AttachInfo();
                attach.code      = ReturnCode.Error.ToInt();
                attach.msg       = ex.Message;
                attach.path      = "";
                attach.name      = "";
                attach.size      = 0;
                attach.extension = "";
                context.Response.Write(JsonHelper.SerializeObject(attach));
            }
        }