Ejemplo n.º 1
0
        public SingleResponse <ArchivoModel> CargaArchivoCobertura()
        {
            SingleResponse <ArchivoModel> response = new SingleResponse <ArchivoModel>();

            try
            {
                string nombreArchivo = "";

                System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                string sPath = WebConfigurationManager.AppSettings["Documentos_coberturas"];

                string filename = "";
                for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
                {
                    System.Web.HttpPostedFile hpf = hfc[iCnt];

                    if (hpf.ContentLength > 0)
                    {
                        if (!Directory.Exists(sPath))
                        {
                            Directory.CreateDirectory(sPath);
                        }
                        if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                        {
                            hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                            nombreArchivo = Path.GetFileName(hpf.FileName);
                        }
                        else
                        {
                            StringBuilder archivoBuilder = new StringBuilder();
                            archivoBuilder.Append(sPath);
                            archivoBuilder.Append(Path.GetFileNameWithoutExtension(hpf.FileName));
                            archivoBuilder.Append("_");
                            archivoBuilder.Append(DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss"));
                            archivoBuilder.Append(Path.GetExtension(hpf.FileName));
                            filename = archivoBuilder.ToString();
                            hpf.SaveAs(filename);
                            var a = Path.GetFileNameWithoutExtension(hpf.FileName);
                            nombreArchivo = filename.Substring(filename.IndexOf(Path.GetFileNameWithoutExtension(hpf.FileName)));
                        }
                    }
                }

                response.Done(new ArchivoModel
                {
                    Ruta          = sPath + nombreArchivo,
                    NombreArchivo = nombreArchivo
                }, string.Empty);
            }
            catch (DomainException e)
            {
                response.Error(e);
            }
            catch (Exception e)
            {
                response.Error(new DomainException(Codes.ERR_00_00));
            }
            return(response);
        }
Ejemplo n.º 2
0
        public SingleResponse <ArchivoTicketsModel> CargarArchivo()
        {
            SingleResponse <ArchivoTicketsModel> response = new SingleResponse <ArchivoTicketsModel>();

            try
            {
                ArchivoTicketsModel           archivoTicketsModel = new ArchivoTicketsModel();
                System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                string sPath        = WebConfigurationManager.AppSettings["TICKETS_FILES_PATH"] + WebConfigurationManager.AppSettings["TICKETS_FILES_REGISTRO"];
                int    iUploadedCnt = 0;
                string filename     = "";
                for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
                {
                    System.Web.HttpPostedFile hpf = hfc[iCnt];

                    if (hpf.ContentLength > 0)
                    {
                        if (!Directory.Exists(sPath))
                        {
                            Directory.CreateDirectory(sPath);
                        }
                        if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                        {
                            hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        }
                        else
                        {
                            StringBuilder archivoBuilder = new StringBuilder();
                            archivoBuilder.Append(sPath);
                            archivoBuilder.Append(Path.GetFileNameWithoutExtension(hpf.FileName));
                            archivoBuilder.Append("_");
                            archivoBuilder.Append(DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss"));
                            archivoBuilder.Append(Path.GetExtension(hpf.FileName));
                            filename = archivoBuilder.ToString();
                            hpf.SaveAs(filename);
                        }
                        archivoTicketsModel.NombreArchivo = hpf.FileName;
                        iUploadedCnt = iUploadedCnt + 1;
                        archivoTicketsModel.RutaArchivo = sPath;
                        response = iGestionBussiness.GuardarArchivo(archivoTicketsModel);
                    }
                }
            }
            catch (DomainException e)
            {
                response.Error(e);
            }
            catch (Exception e)
            {
                response.Error(new DomainException(Codes.ERR_00_00));
            }
            return(response);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 批量保存文件
        /// </summary>
        /// <param name="AbsoluteFilePath">文件保存路径</param>
        /// <returns>
        ///		0:上传文件成功
        ///		1:上传的文件超过指定大小
        ///		2:上传的文件格式不在指定的后缀名列表中
        ///		3:上传的文件没有后缀名
        ///		4:保存文件出错
        ///		5:没有发现上传的文件
        ///</returns>
        public int FilesNewNameSave(string AbsoluteFilePath)
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            string tmpFileExt = _UploadFileExt;

            string[] strFileExt = tmpFileExt.Split(',');
            if (files.Count < 1)
            {
                files = null;
                return(5);//没有发现上传文件;
            }
            //处理多文件
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile file = files[i];
                //判断文件大小
                if (file.ContentLength > _UpFolderSize * 1024)
                {
                    return(1);
                }
                //检验后缀名,无后缀名的不做处理
                if (file.FileName != String.Empty)
                {
                    if (IsStringExists(System.IO.Path.GetExtension(file.FileName).ToLower().Trim(), strFileExt) == false)
                    {
                        return(2);
                    }
                    //保存文件
                    try
                    {
                        if (AbsoluteFilePath.LastIndexOf("\\") == AbsoluteFilePath.Length)
                        {
                            CreateDirectory(AbsoluteFilePath);
                            file.SaveAs(AbsoluteFilePath + TimeFileName(System.IO.Path.GetExtension(file.FileName)));
                        }
                        else
                        {
                            CreateDirectory(AbsoluteFilePath);
                            file.SaveAs(AbsoluteFilePath + "\\" + TimeFileName(System.IO.Path.GetExtension(file.FileName)));
                        }
                    }
                    //catch(Exception e1)
                    catch
                    {
                        //throw new Exception(e1.Source + e1.Message);
                        return(4);
                    }
                }
            }
            files = null;
            return(0);
        }
        public string UploadFiles()
        {
            int    iUploadedCnt = 0;
            string sPath        = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/UploadTemp/");
            string date = DateTime.Now.Year.ToString();

            sPath = Path.Combine(sPath, date);
            if (!Directory.Exists(sPath))
            {
                Directory.CreateDirectory(sPath);
            }
            date  = DateTime.Now.Month.ToString();
            sPath = Path.Combine(sPath, date);
            if (!Directory.Exists(sPath))
            {
                Directory.CreateDirectory(sPath);
            }
            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];
                if (hpf.ContentLength > 0)
                {
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        iUploadedCnt = iUploadedCnt + 1;
                    }

                    else
                    {
                        FileInfo f = new FileInfo(sPath + Path.GetFileName(hpf.FileName));
                        f.Delete();
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }

            if (iUploadedCnt > 0)
            {
                return(iUploadedCnt + " Files Uploaded Successfully");
            }

            else
            {
                return("Upload Failed");
            }
        }
Ejemplo n.º 5
0
        public string UploadFiles()
        {
            int iUploadedCnt = 0;

            string sPath = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/FileTemps/");


            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;


            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }

            if (iUploadedCnt > 0)
            {
                return(iUploadedCnt + " Files Uploaded Successfully");
            }
            else
            {
                return("Upload Failed");
            }
        }
Ejemplo n.º 6
0
        public HttpResponseMessage Upload(int code)
        {
            int    upLoadcount = 0;
            string sPath       = System.Web.Hosting.HostingEnvironment.MapPath("/HinhAnh/");

            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile file = files[i];
                string fileName = new FileInfo(file.FileName).Name;
                if (file.ContentLength > 0)
                {
                    if (!File.Exists(sPath + Path.GetFileName(fileName)))
                    {
                        Guid   id = Guid.NewGuid();
                        string modifiedFileName = id.ToString() + "_" + fileName;
                        if (!File.Exists(sPath + Path.GetFileName(modifiedFileName)))
                        {
                            file.SaveAs(sPath + Path.GetFileName(modifiedFileName));
                            upLoadcount++;
                            var entity = db.SanPhams.Find(code);
                            entity.HinhAnh = modifiedFileName;
                        }
                    }
                }
            }
            if (upLoadcount > 0)
            {
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK, "Success"));
            }
            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error"));
        }
Ejemplo n.º 7
0
        public string UploadFiles()
        {
            int iUploadedCnt = 0;
            string path = System.Web.Hosting.HostingEnvironment.MapPath("~/uploads");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            for(int iCnt = 0; iCnt < hfc.Count; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if(hpf.ContentLength > 0)
                {
                    string fullPath = Path.Combine(path, Path.GetFileName(hpf.FileName));
                    if (!File.Exists(fullPath))
                    {
                        hpf.SaveAs(fullPath);
                        SaveData(fullPath);
                        iUploadedCnt++;
                        continue;
                    }
                    return Path.GetFileName(hpf.FileName) + " does not exists";
                }
            }

            if(iUploadedCnt > 0)
            {
                return iUploadedCnt + " Files Uploaded Successfully";
            }
            else
            {
                return "Uploaded Failed";
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 保存文件并返回文件路径
        /// </summary>
        /// <param name="file1"></param>
        /// <returns></returns>
        public string uploadFile(System.Web.HttpPostedFile file1)
        {
            if (file1 == null)
            {
                throw new Exception("缺少上传文件!");
            }
            //判断文件类型是否正确或者用后缀
            string ext    = System.IO.Path.GetExtension(file1.FileName).ToLower();
            string suffix = ".xls,.xlsx,";

            if (suffix.ToLower().IndexOf(ext) == -1)
            {
                throw new Exception("文件格式错误!");
            }
            //获取文件存放的服务器目录
            string path = PC.Common.Config.objConfig.getStringAppSetings("tempFile");

            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            //检验文件是否存在,存在则另起名保存
            string filename = System.IO.Path.GetFileName(file1.FileName);

            if (existSameFile(path, filename))
            {
                filename = System.DateTime.Now.ToString("yyMMddHHmmss") + "_" + filename;
            }
            file1.SaveAs(path + filename);
            return(path + filename);
        }
Ejemplo n.º 9
0
        public static FileMessage UploadFile(ref System.Web.HttpPostedFile file, String filePath)
        {
            Impersonate oImpersonate    = new Impersonate();
            Boolean     wasImpersonated = Impersonate.isImpersonated();

            try
            {
                if (!wasImpersonated && oImpersonate.ImpersonateValidUser() == FileMessage.ImpersonationFailed)
                {
                    return(FileMessage.ImpersonationFailed);
                }
                else
                {
                    file.SaveAs(filePath);
                    return(FileMessage.FileCreated);
                }
            }
            catch
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
                return(FileMessage.Catch);
            }
            finally
            {
                if (!wasImpersonated)
                {
                    oImpersonate.UndoImpersonation();
                }
            }
        }
Ejemplo n.º 10
0
 public ActionResult ImgUpload()
 {
     try
     {
         System.Web.HttpRequest    request    = System.Web.HttpContext.Current.Request;
         System.Web.HttpPostedFile uploadFile = request.Files[0];
         // 文件上传后的保存路径
         string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + ".jpg";//图片名称
         // img-path images路径--Start
         string filePathDate = DateTime.Now.ToShortDateString().ToString();
         filePathDate = filePathDate.Replace("-", "/");
         // img-path images路径--End
         string imagePath = "/image/" + "/" + filePathDate + "/";
         string filepath  = Server.MapPath("~/upload") + imagePath;
         if (!Directory.Exists(filepath))
         {
             Directory.CreateDirectory(filepath);
         }
         uploadFile.SaveAs(filepath + filename);
         //HttpPostedFileBase File1 = Request.Files[0];
         //检查上传的物理路径是否存在,不存在则创建
         string imgurl = HostUrl() + "/upload" + imagePath + filename;
         return(Json(new { success = true, imgurl = imgurl }));
     }
     catch (Exception ex)
     {
         return(Json(new { success = true, msg = ex.Message }));
     }
 }
        public HttpResponseMessage Uploadfile()
        {
            object obj;
            int    iUploadedCnt = 0;
            string sPath        = "";

            try
            {
                sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/");

                System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                System.Web.HttpPostedFile     hpf = hfc[0];
                if (hpf.ContentLength > 0)
                {
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
                obj = new { StatusCode = 200, data = "http://localhost:59728/Images/" + hpf.FileName };
            }
            catch (Exception ex)
            {
                obj = new { StatusCode = 500, data = new List <Book>() };
            }

            return(Request.CreateResponse(obj));


            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
        }
Ejemplo n.º 12
0
        public UploadFileOutput UploadWithStream()
        {
            System.Web.HttpContext context    = System.Web.HttpContext.Current;
            UploadFileOutput       returnNode = new UploadFileOutput();

            string date = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            int    cout = context.Request.Files.Count;

            if (cout > 0)
            {
                System.Web.HttpPostedFile hpf = context.Request.Files[0];
                if (hpf != null)
                {
                    string fileExt = Path.GetExtension(hpf.FileName).ToLower();
                    //只能上传文件,过滤不可上传的文件类型

                    //string fileFilt = ".gif|.jpg|.bmp|.jpeg|.png";
                    //if (fileFilt.IndexOf(fileExt) <= -1)
                    //{
                    //    return "1";
                    //}

                    //判断文件大小
                    int length = hpf.ContentLength;
                    if (length > 16240000)
                    {
                        returnNode.error = 2;
                        returnNode.msg   = "文件大小超出限制";
                        return(returnNode);
                    }

                    if (localPath.Trim().Length == 0)
                    {
                        localPath = System.Web.HttpContext.Current.Server.MapPath("/");
                    }

                    DateTime dt               = DateTime.Now;
                    string   folderPath       = dt.Year + "\\" + dt.Month + "\\" + dt.Day + "\\";
                    string   serverFolderPath = "/" + dt.Year + "/" + dt.Month + "/" + dt.Day + "/";
                    if (Directory.Exists(localPath + folderPath) == false)//如果不存在就创建file文件夹
                    {
                        Directory.CreateDirectory(localPath + folderPath);
                    }

                    string fileName = System.Guid.NewGuid().ToString() + Path.GetExtension(hpf.FileName);

                    string filePath = localPath + folderPath + fileName;
                    hpf.SaveAs(filePath);
                    returnNode.error = 0;
                    returnNode.msg   = "上传成功";
                    returnNode.url   = serverPath + serverFolderPath + fileName;
                    return(returnNode);
                }
            }
            returnNode.error = 3;
            returnNode.msg   = "没有获取到文件";
            return(returnNode);
        }
Ejemplo n.º 13
0
        public ResponseViewModel UploadImage()
        {
            try
            {
                var    info  = new ResponseViewModel();
                string sPath = "~/UploadFile/WorkPhotos/";
                sPath = System.Web.Hosting.HostingEnvironment.MapPath(sPath);
                Guid guid = Guid.NewGuid();
                System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                var FolderName = System.Web.HttpContext.Current.Request.Form;
                //import.Id = Convert.ToInt64(FolderName["ContactID"]);
                var PhotoPath = "";
                for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
                {
                    System.Web.HttpPostedFile hpf = hfc[iCnt];

                    if (hpf.ContentLength > 0)
                    {
                        //var savedFilePath = (System.Web.HttpContext.Current.Request.PhysicalApplicationPath + @"UploadFile\WorkPhotos\");
                        var savedFilePath = sPath;
                        if (!Directory.Exists(savedFilePath))
                        {
                            Directory.CreateDirectory(savedFilePath);
                        }
                        var extn = Path.GetExtension(hpf.FileName);

                        //PhotoPath = sPath + "\\" + guid + ".jpg";
                        PhotoPath = sPath + "\\" + guid + extn;
                        if (!File.Exists(PhotoPath))
                        {
                            hpf.SaveAs(PhotoPath);
                            //PhotoPath = hpf.FileName;
                            PhotoPath = "/UploadFile/WorkPhotos/" + guid + extn;
                        }
                    }
                }

                responseViewModel.Content   = PhotoPath;
                responseViewModel.IsSuccess = true;
            }
            catch (Exception ex)
            {
                responseViewModel.IsSuccess = false;
                if (responseViewModel.ReturnMessage != null && responseViewModel.ReturnMessage.Count > 0)
                {
                    responseViewModel.ReturnMessage.Add(ex.Message);
                }
                else
                {
                    responseViewModel.ReturnMessage = new List <string>();
                    responseViewModel.ReturnMessage.Add(ex.Message);
                }
            }
            return(responseViewModel);
        }
Ejemplo n.º 14
0
        public void UploadFiles()
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";
            //sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/LC/");

            //sPath = @"D:/Upload/LC/";

            //var directory = @"D:/Upload/LC/";
            CmnDocumentPath objDocumentPath = objRequisitionService.GetUploadPath(TransactionTypeID);

            //string documentPath = objDocumentPath.PhysicalPath.ToString();
            var directory = @objDocumentPath.PhysicalPath;

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        //hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        string exttension = System.IO.Path.GetExtension(hpf.FileName);
                        int    fileSerial = iCnt + 1;
                        hpf.SaveAs(directory + SPRNo + "_Doc_" + fileSerial + exttension);
                        iUploadedCnt = iUploadedCnt + 1;
                        hpf.InputStream.Dispose();
                    }
                }
            }
            SPRNo = "";

            // RETURN A MESSAGE (OPTIONAL).
            //if (iUploadedCnt > 0)
            //{
            //    return iUploadedCnt + " Files Uploaded Successfully";
            //}
            //else
            //{
            //    return "Upload Failed";
            //}
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public string UploadExcel()
        {
            bool   result     = false;
            string resultPath = string.Empty;

            try
            {
                System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                for (int fileCount = 0; fileCount < files.Count; fileCount++)
                {
                    System.Web.HttpPostedFile postedfile = files[fileCount];
                    string fileExtend = System.IO.Path.GetExtension(postedfile.FileName);
                    if (fileExtend == ".xlsx" || fileExtend == ".xls" || fileExtend == ".csv")
                    {
                        string fileName = System.IO.Path.GetFileName(postedfile.FileName);
                        if (!String.IsNullOrEmpty(fileName))
                        {
                            string fileExtension = System.IO.Path.GetExtension(fileName);    //获取文件类型

                            //上传目录
                            string directory = System.Web.HttpContext.Current.Server.MapPath("/UpLoadFiles/");
                            //文件全路径
                            string path = directory + fileName;

                            //判断目录是否存在
                            if (!Directory.Exists(directory))
                            {
                                Directory.CreateDirectory(directory);
                            }
                            //文件存在就删除文件
                            if (File.Exists(path))
                            {
                                File.Delete(path);
                            }
                            //上传到服务器的路径
                            postedfile.SaveAs(path);
                            result     = true;
                            resultPath = path;
                        }
                    }
                    else
                    {
                        result     = false;
                        resultPath = "";
                    }
                }
            }
            catch (Exception)
            {
                result     = false;
                resultPath = "";
            }
            return(resultPath);
        }
Ejemplo n.º 16
0
 private bool SaveFileESX(string fileName, System.Web.HttpPostedFile uploadFile)
 {
     try
     {
         uploadFile.SaveAs(fileName);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Ejemplo n.º 17
0
 private bool SaveFileSplitInvoice(string fileName, System.Web.HttpPostedFile uploadFile)
 {
     try
     {
         if (uploadFile.ContentLength > 0)
         {
             uploadFile.SaveAs(fileName);
         }
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
        //[AllowAnonymous]
        public string UploadPhoto()
        {
            string sPath = System.Web.Hosting.HostingEnvironment.MapPath("/CoverPhoto/");

            System.Web.HttpPostedFile file = System.Web.HttpContext.Current.Request.Files[0];

            string fileName = new FileInfo(file.FileName).Name;

            Guid id = new Guid();

            string modifiedFileName = "http://localhost:1875/CoverPhoto/" + id.ToString() + "_" + fileName;

            file.SaveAs(sPath + Path.GetFileName(modifiedFileName));

            return(modifiedFileName);
        }
        private string SaveDoc(System.Web.HttpFileCollection attachments)
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath    = "";
            string filePath = "";
            string appPath  = ConfigurationManager.AppSettings["AppPath"];

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/ProductDocuments/AffiliateProgram/");

            System.Web.HttpFileCollection hfc = attachments;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    DirectoryInfo folder = new DirectoryInfo(sPath);
                    if (!folder.Exists)
                    {
                        folder.Create();
                    }

                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        filePath     = sPath + Path.GetFileName(hpf.FileName);
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }

            // RETURN A MESSAGE (OPTIONAL).
            if (iUploadedCnt > 0)
            {
                return(filePath);
            }
            else
            {
                return("");
            }
        }
        public JObject UploadFile()
        {
            JObject obj = new JObject();

            try
            {
                string uploadPath  = System.Web.HttpContext.Current.Server.MapPath("~/ApiUploadFile/");
                string uploadPath2 = Environment.CurrentDirectory;
                System.Web.HttpRequest        request        = System.Web.HttpContext.Current.Request;
                System.Web.HttpFileCollection fileCollection = request.Files;
                // 判断是否有文件
                if (fileCollection.Count > 0)
                {
                    // 获取文件
                    System.Web.HttpPostedFile httpPostedFile = fileCollection[0];
                    string fileName      = httpPostedFile.FileName;              // 文件名称
                    string fileExtension = Path.GetExtension(fileName);          // 文件扩展名
                    string filePath      = uploadPath + httpPostedFile.FileName; // 上传路径
                    // 如果目录不存在则要先创建
                    if (!Directory.Exists(uploadPath))
                    {
                        Directory.CreateDirectory(uploadPath);
                    }
                    // 保存新的文件
                    while (File.Exists(filePath))
                    {
                        fileName = Guid.NewGuid().ToString() + fileExtension;
                        filePath = uploadPath + fileName;
                    }
                    httpPostedFile.SaveAs(filePath);
                    obj.Add("success", true);
                    obj.Add("fileName", fileName);
                }
                else
                {
                    obj.Add("success", false);
                    obj.Add("fileName", "文件不存在");
                }
            }
            catch (Exception ex)
            {
                obj.Add("success", false);
                obj.Add("fileName", ex.Message);
            }
            return(obj);
        }
Ejemplo n.º 21
0
        public async Task <HttpResponseMessage> CreateTask()
        {
            httpResponseMessage = new HttpResponseMessage();
            int iUploadedCnt = 0;

            List <string> list = new List <string>();
            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Documents/");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        var p = (Path.Combine(sPath + (hpf.FileName)));
                        list.Add(p);
                    }
                }
            }


            try
            {
                var modelData          = JsonConvert.DeserializeObject <TaskRequest>(System.Web.HttpContext.Current.Request.Form["data"]);
                var createTaskResponse = await _ICommonService.CreateTask(modelData, list);

                httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK, createTaskResponse);
            }
            catch (Exception ex)
            {
            }
            return(httpResponseMessage);
        }
        public IHttpActionResult UploadFiles()
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/images/");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            System.Web.HttpPostedFile     hpf = null;
            string filename = "";

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        filename = sPath + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetFileName(hpf.FileName);
                        hpf.SaveAs(filename);
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }

            // RETURN A MESSAGE (OPTIONAL).
            if (iUploadedCnt > 0)
            {
                Image img = new Image();
                img.ImageUrl = filename.Remove(0, sPath.Length);
                return(Ok(img));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 23
0
        public string UploadFiles()
        {
            Uploader u            = new Uploader();
            int      iUploadedCnt = 0;
            string   sPath        = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Images/");
            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)

                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        string filen = Path.GetFileName(hpf.FileName);

                        // SAVE THE FILES IN THE FOLDER.
                        hpf.SaveAs(sPath + filen);
                        u.ReSizeImage(sPath + filen, sPath + "Thump/", 120);

                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }


            // RETURN A MESSAGE.
            if (iUploadedCnt > 0)
            {
                return(iUploadedCnt + " billeder uploadet!");
            }
            else
            {
                return("Upload Failed!!!");
            }
        }
        public bool UploadFile(System.Web.HttpPostedFile httpPostedFile)
        {
            string targetFolder = System.Web.HttpContext.Current.Server.MapPath("~/");
            string fileName     = !string.IsNullOrEmpty(ReportDesignerHelper.SaveFileName) ? ReportDesignerHelper.SaveFileName : System.IO.Path.GetFileName(httpPostedFile.FileName);

            targetFolder += "Cache";

            if (!System.IO.Directory.Exists(targetFolder))
            {
                System.IO.Directory.CreateDirectory(targetFolder);
            }

            if (!System.IO.Directory.Exists(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken))
            {
                System.IO.Directory.CreateDirectory(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken);
            }

            httpPostedFile.SaveAs(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken + "\\" + fileName);
            return(true);
        }
Ejemplo n.º 25
0
        public void Attach(System.Web.HttpPostedFile file)
        {
            if (file == null || file.ContentLength == 0)
            {
                return;
            }

            string path = WebContext.Server.MapPath("~/PRV/temp");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(path, lw.Utils.StringUtils.GetFileName(file.FileName));

            file.SaveAs(path);

            Attach(path);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// 覆盖保存文件
        /// </summary>
        /// <param name="fileIndex">文件对象索引</param>
        /// <param name="AbsoluteFileName">文件保存路径及文件名</param>
        /// <returns>
        ///		0:上传文件成功
        ///		1:上传的文件超过指定大小
        ///		2:上传的文件格式不在指定的后缀名列表中
        ///		3:上传的文件没有后缀名
        ///		4:保存文件出错
        ///		5:文件对象索引错误
        ///</returns>
        public int Save(int fileIndex, string AbsoluteFileName)
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            string tmpFileExt = _UploadFileExt;

            string[] strFileExt = tmpFileExt.Split(',');
            if (fileIndex < 0 || fileIndex >= files.Count)
            {
                files = null;
                return(5);
            }
            System.Web.HttpPostedFile file = files[fileIndex];
            //判断文件大小
            if (file.ContentLength > _UpFolderSize * 1024)
            {
                return(1);
            }
            //检验后缀名
            if (file.FileName != String.Empty)
            {
                if (IsStringExists(System.IO.Path.GetExtension(file.FileName).ToLower().Trim(), strFileExt) == false)
                {
                    return(2);
                }
            }
            else
            {
                return(3);
            }
            //保存文件
            try
            {
                //FileDelete(AbsoluteFileName);
                file.SaveAs(AbsoluteFileName);
            }
            catch
            {
                return(4);
            }
            return(0);
        }
Ejemplo n.º 27
0
        public HttpResponseMessage UploadAvatar()
        {
            int    iUploadedCnt = 0;
            string sPath = string.Empty; string result = string.Empty;

            try
            {
                // var directory = @"D:/Upload/Avatar/";
                var directory = @"E:/Upload/Avatar/";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
                for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
                {
                    System.Web.HttpPostedFile hpf = hfc[iCnt];
                    if (hpf.ContentLength > 0)
                    {
                        if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                        {
                            string newName    = DateTime.Now.ToString("ddMMMyyhhmmsstt");
                            string exttension = System.IO.Path.GetExtension(hpf.FileName);
                            int    fileSerial = iCnt + 1;
                            string fileName   = "Avt_" + newName + fileSerial + exttension;
                            string filePath   = directory + fileName;
                            hpf.SaveAs(filePath);
                            result       = fileName;
                            iUploadedCnt = iUploadedCnt + 1;
                            hpf.InputStream.Dispose();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
Ejemplo n.º 28
0
        public string UploadLessionImage()
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/img/LessionImage");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    var path = Path.Combine(sPath, hpf.FileName);
                    hpf.SaveAs(path);
                    iUploadedCnt = iUploadedCnt + 1;
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    //if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    //{
                    //    // SAVE THE FILES IN THE FOLDER.
                    //    hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                    //
                    //}
                }
            }

            // RETURN A MESSAGE (OPTIONAL).
            if (iUploadedCnt > 0)
            {
                return("OK");
            }
            else
            {
                return("Fail");
            }
        }
        public string UploadFile()
        {
            int iUploadedCnt = 0;

            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";

            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/");

            string sessionKey = System.Web.HttpContext.Current.Request.Form.Get("SessionKey");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];

                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(sPath + Path.GetFileName(hpf.FileName)))
                    {
                        // SAVE THE FILES IN THE FOLDER.
                        hpf.SaveAs(sPath + Path.GetFileName(hpf.FileName));
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }

            // RETURN A MESSAGE (OPTIONAL).
            if (iUploadedCnt > 0)
            {
                return(iUploadedCnt + " Files Uploaded Successfully");
            }
            else
            {
                return("Upload Failed");
            }
        }
Ejemplo n.º 30
0
        private string SaveFile(System.Web.HttpPostedFile file, String baseVirtualPath, out String extension)
        {
            extension = "";
            try
            {
                extension = Path.GetExtension(file.FileName);
                //Generate a unique name using Guid
                var uniqueName = Guid.NewGuid().ToString();
                //Get physical path of our folder where we want to save images
                var rootPath = System.Web.HttpContext.Current.Server.MapPath(baseVirtualPath); //"~/UploadedFilesTemp"

                var fileSavePath = System.IO.Path.Combine(rootPath, uniqueName + extension);
                // Save the uploaded file to "UploadedFiles" folder
                file.SaveAs(fileSavePath);

                return(uniqueName);
            }
            catch (Exception ex)
            {
                return("");
            }
        }