Пример #1
0
        public ActionResult AsyncUpload()
        {
            HttpPostedFileBase file = Request.Files[0];

            if (file == null)
            {
                throw new Exception("No file Upload");
            }
            Picture _pic = new Picture();

            _pic.SmallPath = ImgUploadService.imgSingUpload(file, 200, 200);
            _pic.Default   = ImgUploadService.imgDefaultSingUpload(file);
            if (_pictureService.Insert(_pic) != null)
            {
                var result = new PictureViewModel();
                result.SmallPath = _pic.SmallPath;
                result.Id        = _pic.Id;
                return(Json(result, JsonRequestBehavior.AllowGet));
            }

            return(Json(""));
        }
Пример #2
0
 private void InsertPicture(Picture _pic, CategoryViewModel model)
 {
     if (model.Picture != null && model.Picture.ContentLength > 0 && model.PictureId == 0 || model.PictureId == null)
     {
         _pic.Title     = model.Name;
         _pic.SmallPath = ImgUploadService.imgSingUpload(model.Picture, 200, 200);
         _pic.Default   = ImgUploadService.imgDefaultSingUpload(model.Picture);
         _pic.Alt       = model.Name;
         _pic.OrderNo   = 0;
         _pictureService.Insert(_pic);
     }
     if (model.Picture != null && model.Picture.ContentLength > 0 && model.PictureId > 0 && model.PictureId != null)
     {
         _pic.Id        = model.PictureId ?? 0;
         _pic.Title     = model.Name;
         _pic.SmallPath = ImgUploadService.imgSingUpload(model.Picture, 200, 200);
         _pic.Default   = ImgUploadService.imgDefaultSingUpload(model.Picture);
         _pic.Alt       = model.Name;
         _pic.OrderNo   = 0;
         _pictureService.Update(_pic);
     }
 }
Пример #3
0
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="context"></param>
        private static void DeleteFile(HttpContext context)
        {
            try
            {
                FileUploadModel model = new FileUploadModel();
                if (context.Request["fileUploadId"] != null)
                {
                    int fileUploadId = Convert.ToInt32(context.Request["fileUploadId"]);
                    model = new FileUploadData().GetFileById(fileUploadId);
                    if (model != null)
                    {
                        //删除产品子级图片表
                        new B2bComProData().DelProChildImg(fileUploadId);

                        //删除数据库里的附件信息
                        new FileUploadData().DeleteById(fileUploadId);

                        ImgUploadService upload = new ImgUploadService();
                        //删除文件
                        upload.DelFile(model.Savepath);
                        //File.Delete(model.Savepath);
                    }
                    context.Response.Write("success");
                }
                else
                {
                    //int objId = Convert.ToInt32(context.Request["objId"]);
                    //int objType = Convert.ToInt32(context.Request["objType"]);
                    //model = new FileUploadData().GetFileByObjIdType(objId, objType);
                    context.Response.Write("fail");
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("Exception:" + ex.ToString());
            }
        }
Пример #4
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="context"></param>
        private static void UploadFile(HttpContext context)
        {
            try
            {
                HttpPostedFile file = context.Request.Files["FileData"];

                int    objId     = Convert.ToInt32(context.Request["ObjId"]);
                int    ObjType   = Convert.ToInt32(context.Request["ObjType"]);
                string FileType  = Convert.ToString(context.Request["FileType"]);
                int    FileSize  = Convert.ToInt32(context.Request["FileSize"]);
                string CreatorId = context.Request["CreatorId"].ToString();
                //string ObjTypeName = Convert.ToString((context.Request["ObjTypeName"]));
                //string uploadpath = HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\\";
                //string uploadpath = @"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Network Shortcuts\UploadFileTest (10.0.4.18)";
                //string path = @"Z:\";

                //检查文件
                if (FileType != "*" && !FileType.Contains(Path.GetExtension(file.FileName).ToLower()))//验证类型
                {
                    context.Response.Write("typeError");
                    return;
                }
                else if (file.ContentLength > FileSize)//检查文件大小
                {
                    context.Response.Write("sizeError");
                    return;
                }

                if (file != null)
                {
                    /*----已修改(不能用对象初始化器,报代码已经优化,不能计算值)------*/
                    var fileUpload = new FileUploadModel();
                    fileUpload.Objid        = objId;
                    fileUpload.Objtype      = ObjType;
                    fileUpload.Creationip   = HttpContext.Current.Request.UserHostAddress;
                    fileUpload.Creator      = CreatorId;
                    fileUpload.OrigenalName = file.FileName;
                    #region
                    ////走本地
                    //fileUpload = FileUploadBLL.GeneratePathFile(file.FileName, fileUpload);
                    //file.SaveAs(fileUpload.SavePath);//创建文件

                    ////走wcf服务
                    //fileUpload = FileUploadBLL.GeneratePathFile(file.FileName, fileUpload, file.InputStream);

                    ////新方法
                    //string uploadPath =
                    //HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";

                    //if (file != null)
                    //{
                    //    if (!Directory.Exists(uploadPath))
                    //    {
                    //        Directory.CreateDirectory(uploadPath);
                    //    }
                    //    file.SaveAs(uploadPath + file.FileName);
                    //    //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
                    //    context.Response.Write("1");
                    //}
                    //else
                    //{
                    //    context.Response.Write("0");
                    //}
                    #endregion
                    //UpLoadAndSaveImage upload = new UpLoadAndSaveImage();
                    ImgUploadService upload = new ImgUploadService();
                    try
                    {
                        string virPath = "";
                        //string physicPath = HttpContext.Current.Server.MapPath(@context.Request.ApplicationPath + "/" + "UploadFile/");

                        byte[] data = new byte[file.ContentLength];

                        file.InputStream.Read(data, 0, file.ContentLength);

                        string physicPath = upload.UpLoadAndSave(data, ref virPath, Path.GetExtension(file.FileName).ToLower());

                        //文件路径等信息,插入数据库
                        fileUpload.Relativepath = virPath;
                        fileUpload.Savepath     = physicPath;
                        fileUpload.Type         = 0;
                        fileUpload = new FileUploadData().InsertFileUpload(fileUpload);



                        string strFileObject = "[{\"Id\":\"" + fileUpload.Id + "\",\"ObjType\":\"" + fileUpload.Objtype
                                               + "\",\"ObjId\":\"" + fileUpload.Objid + "\",\"FileName\":\"" + fileUpload.OrigenalName
                                               + "\",\"FilePath\":\"" + fileUpload.Relativepath + "\",\"ExtName\":\"" + fileUpload.Extentionname
                                               + "\",\"Type\":\"" + fileUpload.Type
                                               + "\",\"physicPath\":\"" + fileUpload.Savepath
                                               + "\",\"Creator\":\"" + fileUpload.Creator + "\",\"CreationIp\":\"" + fileUpload.Creationip + "\"}]";

                        //context.Response.Write(fileUpload.Id.ToString() + "|" + fileUpload.RelativePath + "|" + fileUpload.OrigenalName);
                        context.Response.Write(strFileObject);
                    }
                    catch (Exception ex)
                    {
                        context.Response.Write(ex.Message);
                        return;
                    }
                }
                else
                {
                    context.Response.Write("noFile");
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("Exception:" + ex.ToString());
            }
        }
Пример #5
0
        public string UpLoadImage(string fileNamePath, HttpContext context)
        {
            try
            {
                HttpPostedFile file = context.Request.Files[0];

                //获得文件扩展名
                string fileNameExt = Path.GetExtension(file.FileName).ToLower();
                //验证合法的文件
                if (CheckImageExt(fileNameExt))
                {
                    ImgUploadService upload = new ImgUploadService();

                    string virPath = "";
                    byte[] data    = new byte[file.ContentLength];

                    file.InputStream.Read(data, 0, file.ContentLength);

                    string physicPath = upload.UpLoadAndSave(data, ref virPath, Path.GetExtension(file.FileName).ToLower());

                    string FullPath = "http://image.etown.cn/UploadFile/" + virPath;

                    return(FullPath);
                }
                else
                {
                    throw new Exception("文件格式非法,请上传.gif, .jpg, .jpeg, .bmp, .png格式的文件。");
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
            #region
            //try
            //{
            //    string serverPath = System.Web.HttpContext.Current.Server.MapPath("~");

            //    string toFilePath = Path.Combine(serverPath, @"UploadFile\");

            //    //获取要保存的文件信息
            //    FileInfo file = new FileInfo(fileNamePath);
            //    //获得文件扩展名
            //    string fileNameExt = file.Extension.ToLower();

            //    //验证合法的文件
            //    if (CheckImageExt(fileNameExt))
            //    {
            //        //生成将要保存的随机文件名
            //        string fileName = GetImageName() + fileNameExt;

            //        //获得要保存的文件路径
            //        string serverFileName = toFilePath + fileName;
            //        //物理完整路径
            //        string toFileFullPath = serverFileName; //HttpContext.Current.Server.MapPath(toFilePath);

            //        //将要保存的完整文件名
            //        string toFile = toFileFullPath;//+ fileName;

            //        ///创建WebClient实例
            //        WebClient myWebClient = new WebClient();
            //        //设定windows网络安全认证   方法1
            //        myWebClient.Credentials = CredentialCache.DefaultCredentials;
            //        ////设定windows网络安全认证   方法2

            //        context.Request.Files[0].SaveAs(toFile);

            //        string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath + string.Format(@"UploadFile/{0}", fileName);

            //        return relativePath;
            //    }
            //    else
            //    {
            //        throw new Exception("文件格式非法,请上传.gif, .jpg, .jpeg, .bmp, .png格式的文件。");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    return ex.Message;
            //}
            #endregion
        }