コード例 #1
0
ファイル: UploadController.cs プロジェクト: tcns/impulse
        public async Task<HttpResponseMessage> PostFormData()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            VideoUnitDTO video = new VideoUnitDTO
            {
                DateLoaded = DateTime.Now,
                GeneratedName = Math.Abs(Guid.NewGuid().GetHashCode()).ToString(),
                Name = "DefaultName",
                UserName = User.Identity.Name
            };
            string root = HttpContext.Current.Server.MapPath("~/Videos");
            var path = "/" + User.Identity.Name + "/" + video.GeneratedName + "/";
            DirectoryInfo directory = Directory.CreateDirectory(root + path);
            var fileName = "Videos" + path;
            var provider = new ImpulseFormDataStreamProvider(directory.FullName);
            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);
                foreach (var file in provider.FileData)
                {
                    if (file.Headers.ContentType.MediaType.Contains("video"))
                    {
                        video.FullPath = "/" + fileName + Path.GetFileName(file.LocalFileName);
                        video.MimeType = file.Headers.ContentType.MediaType;
                    }
                    else
                    {
                        var exceptionMessage = new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType)
                        {
                            ReasonPhrase = "Файл не является видео",
                            Content = new StringContent("Тип загруженного объекта " + file.Headers.ContentType.MediaType)
                        };
                        throw new HttpResponseException(exceptionMessage);
                    }
                }

                return Request.CreateResponse(HttpStatusCode.OK, video);
            }
            catch (HttpResponseException e)
            {
                return Request.CreateErrorResponse(e.Response.StatusCode, e.Response.ReasonPhrase);
            }
        }
コード例 #2
0
ファイル: UploadController.cs プロジェクト: tcns/impulse
 public async Task<HttpResponseMessage> SaveImage()
 {
     string path = "/Images/" + User.Identity.Name + "/";
     string fullPath = HttpContext.Current.Server.MapPath("~" + path);
     if (!Directory.Exists(fullPath))
     {
         Directory.CreateDirectory(fullPath);
     }
     var provider = new ImpulseFormDataStreamProvider(fullPath);
     try
     {
         await Request.Content.ReadAsMultipartAsync(provider);
         path = path + Path.GetFileName(provider.FileData[0].LocalFileName);
         return Request.CreateResponse(HttpStatusCode.OK, path);
     }
     catch (Exception ex) { 
         return Request.CreateResponse(HttpStatusCode.InternalServerError); 
     }
     
     
 }