Ejemplo n.º 1
0
        /// <summary>
        /// 单个文件上传(只获取第一个文件,返回的文件名是文件的md5值),返回为json数据格式,成功返回{status:"success",website:"a.jpg"},失败,返回{status:"error",website:"error"}
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="FilePath">文件路径</param>
        /// <param name="outFileName">返回文件的md5</param>
        /// <returns>返回json状态信息</returns>
        public static string FileUploadSingle(HttpContextBase context, string FilePath, out string outFileName)
        {
            string json = "";
            //找到目标文件对象
            HttpFileCollectionBase hfc = context.Request.Files;
            HttpPostedFileBase     hpf = hfc[0];

            if (hpf.ContentLength > 0)
            {
                //根据文件的md5的hash值做文件名,防止文件的重复和图片的浪费
                string FileName = CreateDateTimeForFileName(hpf.FileName); //CreateDateTimeForFileName(hpf.FileName);//自动生成文件名

                string file = System.IO.Path.Combine(FilePath,
                                                     FileName);
                if (!Directory.Exists(FileOperate.MapPath(FilePath)))
                {
                    Directory.CreateDirectory(FilePath);
                }
                hpf.SaveAs(file);
                json        = "{status:\"success\",website:\"" + FileName + "\"}";
                outFileName = FileName;
            }
            else
            {
                json        = "{status:\"error\",website:\"error\"}";
                outFileName = FilePath;
            }

            return(json);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 是否接连删除该路径下的small,middle中的图片
        /// </summary>
        /// <param name="VirtualFilePath">虚拟路径</param>
        /// <param name="IsDeleteForAll">是否需要删除该目录下的small,middle目录中的缩略图</param>
        public static void FileDelete(string VirtualFilePath, bool IsDeleteForAll)
        {
            //物理路径
            string RealFilePath = HttpContext.Current.Server.MapPath(VirtualFilePath);

            DelFile(RealFilePath);
            //是否需要删除该目录下的small,middle目录中的缩略图
            if (IsDeleteForAll)
            {
                //文件名
                string FileName = Path.GetFileName(RealFilePath);
                //返回文件目录信息
                string FilePath = FileOperate.MapPath(RealFilePath);
                //小图片地址
                string SmallPath = FilePath + "/small/" + FileName;
                //中图片地址
                string MiddlePath = FilePath + "/middle/" + FileName;

                DelFile(SmallPath);
                DelFile(MiddlePath);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 单个文件上传(只获取第一个文件,返回的文件名是文件的md5值),返回为json数据格式,成功返回{status:"success",website:"a.jpg"},失败,返回{status:"error",website:"error"}
        /// </summary>
        /// <param name="context">上下文</param>
        /// <param name="FilePath">文件路径</param>
        /// <param name="outFileName">返回文件的md5</param>
        /// <returns>返回json状态信息</returns>
        public static string FileUploadMulti(HttpContext context, string FilePath, out string[] outFileName)
        {
            string json = "";
            //找到目标文件对象
            HttpFileCollection hfc = context.Request.Files;

            outFileName = new  string[hfc.Count];
            for (int i = 0; i < hfc.Count; i++)
            {
                if (hfc[i].ContentLength > 0)
                {
                    //根据文件的md5的hash值做文件名,防止文件的重复和图片的浪费
                    string FileName = CreateDateTimeForFileName(hfc[i].FileName); //CreateDateTimeForFileName(hpf.FileName);//自动生成文件名

                    string file = System.IO.Path.Combine(FilePath,
                                                         FileName);
                    if (!Directory.Exists(FileOperate.MapPath(file)))
                    {
                        Directory.CreateDirectory(file);
                    }
                    hfc[i].SaveAs(file);
                    outFileName[i] = FileName;
                }
            }
            //如果文件非空,就返回json数据
            if (outFileName.Count() > 0)
            {
                json = "{status:\"success\",website:\"" + string.Join(",", outFileName) + "\"}";
            }
            else
            {
                json = "{status:\"error\",website:\"error\"}";
            }


            return(json);
        }