Exemple #1
0
        private byte[] GetImageBytesFromFilePath(string fileName)
        {
            byte[] content  = null;
            string filePath = ImageUploadHelper.GetUploadRootPath(this.RootPathName) + "Temp\\" + fileName;

            if (File.Exists(filePath))
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    content = fs.ToBytes();
                }
            }

            return(content);
        }
Exemple #2
0
        private void UploadImage()
        {
            const int FileTooLargeError = -2147467259;

            ObjectContextCache.Instance["FileUploadProcessed"] = true;

            HttpRequest  request  = HttpContext.Current.Request;
            HttpResponse response = HttpContext.Current.Response;

            string lockID      = WebUtility.GetRequestQueryValue <string>("lockID", string.Empty);
            string userID      = WebUtility.GetRequestQueryValue <string>("userID", string.Empty);
            string fileName    = WebUtility.GetRequestQueryValue <string>("fileName", string.Empty);
            int    fileMaxSize = WebUtility.GetRequestQueryValue <int>("fileMaxSize", 0);
            string controlID   = WebUtility.GetRequestQueryValue("controlID", string.Empty);

            ExceptionHelper.CheckStringIsNullOrEmpty(fileName, "fileName");

            try
            {
                if (fileMaxSize > 0 && request.ContentLength > fileMaxSize)
                {
                    GenerateErrorInformation(string.Format("文件超过了上传大小的限制{0}字节", fileMaxSize));
                }
                else
                {
                    //不检查锁,沈峥修改
                    //CheckLock(lockID, userID);

                    string uploadPath = ImageUploadHelper.GetUploadRootPath(this.RootPathName);

                    AutoCreateUploadPath(uploadPath);

                    string newID = UuidHelper.NewUuidString();
                    fileName = newID + fileName.Substring(fileName.LastIndexOf('.'));

                    if (WebUtility.GetRequestQueryString("upmethod", "") == "new")
                    {
                        var dialogControlID = WebUtility.GetRequestQueryValue("dialogControlID", string.Empty);
                        request.Files[0].SaveAs(uploadPath + @"Temp\" + fileName);

                        string output = "<script type='text/javascript'>";
                        output += "window.parent.$find('" + dialogControlID + "').onUploadFinish(1)";
                        output += "</script>";

                        response.Write(output);
                    }
                    else if (WebUtility.GetRequestQueryString("upmethod", "") == "ue")
                    {
                        request.Files[0].SaveAs(uploadPath + @"Temp\" + fileName);

                        string output = "<script type='text/javascript'>";
                        output += string.Format("window.parent.onUploadFinish('{0}','{1}')", newID, fileName);
                        output += "</script>";

                        response.Write(output);
                    }
                    else
                    {
                        request.SaveAs(uploadPath + @"Temp\" + fileName, false);
                    }

                    string fileIconPath = FileConfigHelper.GetFileIconPath(fileName);

                    response.AppendHeader("fileIconPath", HttpUtility.UrlEncode("message=" + fileIconPath));

                    string dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    response.AppendHeader("lastUploadTag", "message=" + dateTimeNow);
                    response.AppendHeader("newMaterialID", "message=" + HttpUtility.UrlEncode(newID));
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;

                if (ex is ExternalException && ((ExternalException)(ex)).ErrorCode == FileTooLargeError &&
                    ex.Source == "System.Web")
                {
                    errorMessage = "您上传的文件过大";
                }

                GenerateErrorInformation(errorMessage);
            }
            finally
            {
                try
                {
                    response.End();
                }
                catch { }
            }
        }