Ejemplo n.º 1
0
        public async Task<IHttpActionResult> UploadAssetToLocalFileSystem()
        {
            var retVal = new List<webModel.BlobInfo>();

            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }

            var uploadsPath = HostingEnvironment.MapPath(_uploadsUrl);
            var streamProvider = new CustomMultipartFormDataStreamProvider(uploadsPath);

            await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
            });

            foreach (var fileData in streamProvider.FileData)
            {
                var fileName = fileData.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);

                var blobInfo = new webModel.BlobInfo
                {
                    Name = fileName,
                    Url = VirtualPathUtility.ToAbsolute(_uploadsUrl + fileName),
                    MimeType = MimeTypeResolver.ResolveContentType(fileName)
                };
                retVal.Add(blobInfo);
            }

            return Ok(retVal.ToArray());
        }
        public async Task<IHttpActionResult> Upload()
        {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

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

            var streamProvider = new CustomMultipartFormDataStreamProvider(_packagesPath);
            await Request.Content.ReadAsMultipartAsync(streamProvider);

            var file = streamProvider.FileData.FirstOrDefault();
            if (file != null)
            {
                var descriptor = _packageService.OpenPackage(Path.Combine(_packagesPath, file.LocalFileName));
                if (descriptor != null)
                {
                    var retVal = descriptor.ToWebModel();
                    retVal.FileName = file.LocalFileName;

                    var dependencyErrors = _packageService.GetDependencyErrors(descriptor);
                    retVal.ValidationErrors.AddRange(dependencyErrors);

                    return Ok(retVal);
                }
            }

            return NotFound();
        }