예제 #1
0
        /// <summary>
        /// 图片单个上传
        /// </summary>
        /// <param name="file"></param>
        /// <param name="uploadFolder"></param>
        /// <param name="smallimgURL"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="Prefixed"></param>
        /// <param name="filename">返回的文件名</param>
        /// <returns></returns>
        public static bool UploadImgFile(System.Web.HttpPostedFile file, string uploadFolder, string smallimgURL
                                         , int width, int height
                                         , string Prefixed, ref string filename)
        {
            try
            {
                if (file == null || file.ContentLength == 0)
                {
                    return(false);
                }

                if (uploadFolder.Trim().Length > 0 && !Directory.Exists(uploadFolder))
                {
                    Directory.CreateDirectory(uploadFolder);
                }
                if (smallimgURL.Trim().Length > 0 && !Directory.Exists(smallimgURL))
                {
                    Directory.CreateDirectory(smallimgURL);
                }

                string ContentType = file.ContentType.ToString();
                string FileName    = file.FileName;

                string hz = "";
                if (FileName.IndexOf('.') != -1)
                {
                    hz = FileName.Substring(FileName.LastIndexOf('.'), FileName.Length - FileName.LastIndexOf('.'));
                }

                //if (FileName.IndexOf("\\") != -1)
                //    FileName = FileName.Replace("\"", "'").Substring(FileName.LastIndexOf("\\") + 1, FileName.Length - FileName.LastIndexOf("\\") - 1);
                FileName = Prefixed + System.Guid.NewGuid().ToString().Replace("-", "") + hz;

                int    ContentLength = file.ContentLength;
                string GetType       = file.GetType().ToString();
                string savePath      = uploadFolder + FileName;
                file.SaveAs(savePath);
                filename = FileName;
                if (smallimgURL.Trim().Length > 0)
                {
                    MakeThumbnail(savePath, smallimgURL + FileName, width, height);
                }
                return(true);
            }
            catch { }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// 文件单个上传
        /// </summary>
        /// <param name="file"></param>
        /// <param name="uploadFolder">上传的物理地址</param>
        /// <param name="Prefixed">文件名前辍</param>
        /// <param name="filename">返回的文件名</param>
        /// <returns></returns>
        public static bool UploadFile(System.Web.HttpPostedFile file, string uploadFolder, string Prefixed, ref string filename, ref string resultMsg)
        {
            try
            {
                if (file == null || file.ContentLength == 0)
                {
                    return(false);
                }

                if (uploadFolder.Trim().Length > 0 && !Directory.Exists(uploadFolder))
                {
                    Directory.CreateDirectory(uploadFolder);
                }


                string ContentType = file.ContentType.ToString();
                string FileName    = file.FileName;

                string hz = "";
                if (FileName.IndexOf('.') != -1)
                {
                    hz = FileName.Substring(FileName.LastIndexOf('.'), FileName.Length - FileName.LastIndexOf('.'));
                }

                if (filename.Trim().Length > 0)
                {
                    FileName = filename + hz;
                }

                //if (FileName.IndexOf("\\") != -1)
                //    FileName = FileName.Replace("\"", "'").Substring(FileName.LastIndexOf("\\") + 1, FileName.Length - FileName.LastIndexOf("\\") - 1);


                FileName = Prefixed + System.Guid.NewGuid().ToString().Replace("-", "") + hz;

                int    ContentLength = file.ContentLength;
                string GetType       = file.GetType().ToString();
                string savePath      = uploadFolder + FileName;
                file.SaveAs(savePath);
                filename = FileName;
                return(true);
            }
            catch (Exception e) { resultMsg = e.Message; }
            return(false);
        }
예제 #3
0
        /// <summary>
        /// 图片多个上传
        /// </summary>
        /// <param name="uploadFolder">上传的物理路径</param>
        /// <param name="smallimgURL">缩略图路径(物理路径 如果为空则不压缩)</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="Prefixed">文件名前辍</param>
        /// <param name="filenames">上传后的文件名</param>
        /// <returns></returns>
        public static bool UploadImgFiles(string uploadFolder, string smallimgURL, int width, int height
                                          , string Prefixed, ref List <string> filenames)
        {
            bool result = false;

            try
            {
                if (filenames == null)
                {
                    filenames = new List <string>();
                }

                HttpFileCollection files = HttpContext.Current.Request.Files;
                //string smallimgURL = uploadFolder;
                if (uploadFolder.Trim().Length > 0 && !Directory.Exists(uploadFolder))
                {
                    Directory.CreateDirectory(uploadFolder);
                }
                if (smallimgURL.Trim().Length > 0 && !Directory.Exists(smallimgURL))
                {
                    Directory.CreateDirectory(smallimgURL);
                }
                if (files.Count > 0)
                {
                    string path = uploadFolder;
                    //HttpPostedFile file = files[0];

                    for (int i = 0; i < files.Count; i++)
                    {
                        System.Web.HttpPostedFile file = files[i];

                        if (file == null || file.ContentLength == 0)
                        {
                            filenames.Add(file.FileName);
                            continue;
                        }
                        string ContentType = file.ContentType.ToString();
                        string FileName    = file.FileName;

                        string hz = "";
                        if (FileName.IndexOf('.') != -1)
                        {
                            hz = FileName.Substring(FileName.LastIndexOf('.'), FileName.Length - FileName.LastIndexOf('.'));
                        }

                        //if (FileName.IndexOf("\\") != -1)
                        //    FileName = FileName.Replace("\"", "'").Substring(FileName.LastIndexOf("\\") + 1, FileName.Length - FileName.LastIndexOf("\\") - 1);
                        FileName = Prefixed + System.Guid.NewGuid().ToString().Replace("-", "") + hz;

                        int    ContentLength = file.ContentLength;
                        string GetType       = file.GetType().ToString();
                        string savePath      = path + FileName;
                        file.SaveAs(savePath);
                        filenames.Add(FileName);
                        result = true;
                        if (smallimgURL.Trim().Length > 0)
                        {
                            MakeThumbnail(savePath, smallimgURL + FileName, width, height);
                        }
                        //bool ismake = Common.Library.MakeThumbnail(savePath, smallimgURL + filename, 500, 400);
                    }
                }
            }
            catch { }
            return(result);
        }