private void UploadFile()
        {
            HttpRequest    request        = HttpContext.Current.Request;
            HttpPostedFile httpPostedFile = request.Files["fileUpload"];
            string         realPath       = TextUtility.GetRealPath(this.m_folderPath);

            if (httpPostedFile.ContentLength == 0)
            {
                this.m_value = "请先选择文件";
                return;
            }
            if (httpPostedFile.ContentLength > 4096000)
            {
                this.m_value = "文件过大,无法进行上传";
                return;
            }
            if (httpPostedFile.ContentType.ToUpper().IndexOf("IMAGE") == -1)
            {
                this.m_value = "请选择图片文件";
                return;
            }
            string fileName = Path.GetFileName(httpPostedFile.FileName);

            if (File.Exists(realPath + "\\" + fileName))
            {
                Random random = new Random();
                string text   = string.Concat(new object[]
                {
                    Path.GetFileNameWithoutExtension(fileName),
                    "_",
                    random.Next(1, 1000),
                    Path.GetExtension(fileName)
                });
                try
                {
                    httpPostedFile.SaveAs(realPath + "\\" + text);
                    this.m_value          = "上传的文件名已存在, 自动重命名为: " + text;
                    this.m_uploadFilePath = TextUtility.AddLast(this.m_folderPath, "/") + text;
                    return;
                }
                catch
                {
                    this.m_value = "写入文件失败, 权限不足";
                    return;
                }
            }
            try
            {
                httpPostedFile.SaveAs(realPath + "\\" + fileName);
                this.m_value          = "上传文件完毕, 文件名为: " + fileName;
                this.m_uploadFilePath = TextUtility.AddLast(this.m_folderPath, "/") + fileName;
            }
            catch
            {
                this.m_value = "写入文件失败, 权限不足";
            }
        }
Exemple #2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        private void UploadFile()
        {
            HttpRequest    Request    = HttpContext.Current.Request;
            HttpPostedFile fileUpload = Request.Files["fileUpload"];

            string serverPath = TextUtility.GetRealPath(m_folderPath);

            if (fileUpload.ContentLength == 0)
            {
                m_value = "请先选择文件";
            }
            else if (fileUpload.ContentLength > 4096000)
            {
                m_value = "文件过大,无法进行上传";
            }
            else if (fileUpload.ContentType.ToUpper().IndexOf("IMAGE") == -1)
            {
                m_value = "请选择图片文件";
            }
            else
            {
                // HttpContext.Current.Server.ScriptTimeout = 600;
                string fileName = Path.GetFileName(fileUpload.FileName);

                if (File.Exists(serverPath + "\\" + fileName))
                {
                    Random rnd         = new Random();
                    string newFileName = Path.GetFileNameWithoutExtension(fileName) + "_" + rnd.Next(1, 1000) + Path.GetExtension(fileName);

                    try
                    {
                        fileUpload.SaveAs(serverPath + "\\" + newFileName);
                        m_value          = "上传的文件名已存在, 自动重命名为: " + newFileName;
                        m_uploadFilePath = TextUtility.AddLast(m_folderPath, "/") + newFileName;
                    }
                    catch
                    {
                        m_value = "写入文件失败, 权限不足";
                    }
                }
                else
                {
                    try
                    {
                        fileUpload.SaveAs(serverPath + "\\" + fileName);
                        m_value          = "上传文件完毕, 文件名为: " + fileName;
                        m_uploadFilePath = TextUtility.AddLast(m_folderPath, "/") + fileName;
                    }
                    catch
                    {
                        m_value = "写入文件失败, 权限不足";
                    }
                }
            }
        }
        private void UploadFile()
        {
            HttpPostedFile item     = HttpContext.Current.Request.Files["fileUpload"];
            string         realPath = TextUtility.GetRealPath(this.m_folderPath);

            if (item.ContentLength == 0)
            {
                this.m_value = "请先选择文件";
                return;
            }
            if (item.ContentLength > 4096000)
            {
                this.m_value = "文件过大,无法进行上传";
                return;
            }
            if (item.ContentType.ToUpper().IndexOf("IMAGE") == -1)
            {
                this.m_value = "请选择图片文件";
                return;
            }
            string fileName = Path.GetFileName(item.FileName);

            if (!File.Exists(string.Concat(realPath, "\\", fileName)))
            {
                try
                {
                    item.SaveAs(string.Concat(realPath, "\\", fileName));
                    this.m_value          = string.Concat("上传文件完毕, 文件名为: ", fileName);
                    this.m_uploadFilePath = string.Concat(TextUtility.AddLast(this.m_folderPath, "/"), fileName);
                }
                catch
                {
                    this.m_value = "写入文件失败, 权限不足";
                }
            }
            else
            {
                Random   random = new Random();
                object[] fileNameWithoutExtension = new object[] { Path.GetFileNameWithoutExtension(fileName), "_", random.Next(1, 1000), Path.GetExtension(fileName) };
                string   str = string.Concat(fileNameWithoutExtension);
                try
                {
                    item.SaveAs(string.Concat(realPath, "\\", str));
                    this.m_value          = string.Concat("上传的文件名已存在, 自动重命名为: ", str);
                    this.m_uploadFilePath = string.Concat(TextUtility.AddLast(this.m_folderPath, "/"), str);
                }
                catch
                {
                    this.m_value = "写入文件失败, 权限不足";
                }
            }
        }