示例#1
0
        /// <inheritdoc />
        public override object GetSubmittedValue()
        {
            IEnumerable <HttpPostedFile> httpPostedFiles = Enumerable.Empty <HttpPostedFile>();
            HttpFileCollection           files           = HttpContext.Current.Request.Files;

            if (files == null)
            {
                return((object)httpPostedFiles);
            }
            IEnumerable <string>  strings            = files.AllKeys.Where(key => key.StartsWith(FormElement.ElementName));
            List <HttpPostedFile> httpPostedFileList = new List <HttpPostedFile>();

            foreach (string index in strings)
            {
                IEnumerable <HttpPostedFile> httpPostedFilesForIndex = files.GetMultiple(index);
                foreach (var httpPostedFile in httpPostedFilesForIndex)
                {
                    if (httpPostedFile != null && !string.IsNullOrEmpty(httpPostedFile.FileName))
                    {
                        httpPostedFileList.Add(httpPostedFile);
                    }
                }
            }
            if (httpPostedFileList.Count > 0)
            {
                return((object)httpPostedFileList);
            }
            return((object)HttpContext.Current.Request.Form[this.FormElement.ElementName]);
        }
示例#2
0
        /// <summary>
        /// Upload all file of current page
        /// </summary>
        /// <param name="httpFiles"> this.Request.Files </param>
        /// <param name="savePath"> </param>
        public void UploadFile(HttpFileCollection httpFiles, string savePath)
        {
            if (httpFiles == null || httpFiles.Count <= 0)
            {
                return;
            }

            try
            {
                var getKeys = httpFiles.AllKeys.Distinct();
                foreach (var key in getKeys)
                {
                    var files = httpFiles.GetMultiple(key);
                    foreach (HttpPostedFile item in files)
                    {
                        var path = savePath + item.FileName;
                        if (!File.Exists(path))
                        {
                            File.Delete(path);
                        }

                        item.SaveAs(path);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#3
0
        // POST: api/Routes
        public IHttpActionResult Post()
        {
            string         urlimg, urlkml;
            HttpPostedFile uploadImage         = null;
            HttpPostedFile uploadKML           = null;
            IEnumerable <HttpPostedFile> ffile = null;
            HttpFileCollection           files = HttpContext.Current.Request.Files;

            uploadImage = files.Get("uploadImage");
            uploadKML   = files.Get("uploadKML");
            ffile       = files.GetMultiple("ffile");
            int nfiles = files.Count;

            string strName        = HttpContext.Current.Request.Form.Get("Name");
            string strDescription = HttpContext.Current.Request.Form.Get("Description");

            if (strName == "")
            {
                return(Content(HttpStatusCode.BadRequest, "Не указано название карты"));
            }
            if (uploadImage == null || uploadKML == null)
            {
                return(Content(HttpStatusCode.BadRequest, "Нет файла изображения маршрута или файла KML"));
            }
            // получаем имя файла из переданных параметров
            string fileNameImage = System.IO.Path.GetFileName(uploadImage.FileName);
            string fileNameKML   = System.IO.Path.GetFileName(uploadKML.FileName);

            //формируем имя файла для сохранения в БД
            urlimg = String.Format("{0}_{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), Guid.NewGuid(), Path.GetExtension(fileNameImage));
            urlkml = String.Format("{0}_{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), Guid.NewGuid(), Path.GetExtension(fileNameKML));
            List <Photo> photos = new List <Photo>();

            if (ffile != null)
            {
                foreach (HttpPostedFile photoFile in ffile)
                {
                    string photoNameImage = System.IO.Path.GetFileName(photoFile.FileName);
                    string urlphoto       = String.Format("{0}_{1}{2}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), Guid.NewGuid(), Path.GetExtension(photoNameImage));
                    Photo  photo          = new Photo();
                    photo.PhotoName    = urlphoto;
                    photo.Photocreated = DateTime.Now;
                    photo.Description  = photoNameImage;
                    photos.Add(photo);
                    photoFile.SaveAs(HttpContext.Current.Server.MapPath("~/Contents/Files/Photo/" + urlphoto));
                }
            }
            Route route = new Route();

            try
            {
                route.Name        = strName;
                route.Description = strDescription;
                route.RouteImage  = urlimg;
                route.RouteKML    = urlkml;
                db.Routes.Add(route);
                int saved = db.SaveChanges();
                if (saved > 0)
                {
                    uploadImage.SaveAs(HttpContext.Current.Server.MapPath("~/Contents/Files/Img/" + urlimg));
                    uploadKML.SaveAs(HttpContext.Current.Server.MapPath("~/Contents/Files/Kml/" + urlkml));
                    foreach (var p in photos)
                    {
                        p.Route = route;
                    }
                }
                db.Photos.AddRange(photos);
                saved = db.SaveChanges();
                if (saved < 1)
                {
                    foreach (var photo in photos)
                    {
                        string filephoto = photo.PhotoName;
                        try
                        {
                            System.IO.File.Delete(HttpContext.Current.Server.MapPath("~/Contents/Files/Photo/" + filephoto));
                        }
                        catch (Exception e)
                        {
                            return(Content(HttpStatusCode.BadRequest, "Ошибка удаления файла"));
                        }
                    }
                }

                return(Json("Маршрут успешно добавлен."));
            }
            catch (Exception e) { return(Content(HttpStatusCode.BadRequest, "Не удалось добавить запись")); }
        }