Beispiel #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> 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> UploadAsset([FromUri] string folderUrl, [FromUri] string url = null)
        {
            if (url == null && !Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            if (url != null)
            {
                using (var client = new WebClient())
                {
                    var uploadInfo = new UploadStreamInfo
                    {
                        FileByteStream = client.OpenRead(url),
                        FolderName     = folderUrl,
                        FileName       = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url))
                    };

                    var key    = _blobProvider.Upload(uploadInfo);
                    var retVal = new webModel.BlobInfo
                    {
                        Name        = uploadInfo.FileName,
                        RelativeUrl = key,
                        Url         = _urlResolver.GetAbsoluteUrl(key)
                    };
                    return(Ok(retVal));
                }
            }
            else
            {
                var blobMultipartProvider = new BlobStorageMultipartProvider(_blobProvider, _tempPath, folderUrl);
                await Request.Content.ReadAsMultipartAsync(blobMultipartProvider);

                var retVal = new List <webModel.BlobInfo>();

                foreach (var blobInfo in blobMultipartProvider.BlobInfos)
                {
                    retVal.Add(new webModel.BlobInfo
                    {
                        Name        = blobInfo.FileName,
                        Size        = blobInfo.Size.ToString(),
                        MimeType    = blobInfo.ContentType,
                        RelativeUrl = blobInfo.Key,
                        Url         = _urlResolver.GetAbsoluteUrl(blobInfo.Key)
                    });
                }

                return(Ok(retVal.ToArray()));
            }
        }
        public async Task<IHttpActionResult> UploadAsset([FromUri] string folderUrl, [FromUri]string url = null)
        {
            if (url == null && !Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            if (url != null)
            {
                var fileName = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url));
                var fileUrl = folderUrl + "/" + fileName;
                using (var client = new WebClient())
                using (var blobStream = _blobProvider.OpenWrite(fileUrl))
                using (var remoteStream = client.OpenRead(url))
                {
                    remoteStream.CopyTo(blobStream);

                    var retVal = new webModel.BlobInfo
                    {
                        Name = fileName,
                        RelativeUrl = fileUrl,
                        Url = _urlResolver.GetAbsoluteUrl(fileUrl)
                    };
                    return Ok(retVal);
                }
            }
            else
            {
                var blobMultipartProvider = new BlobStorageMultipartProvider(_blobProvider, _urlResolver, folderUrl);
                await Request.Content.ReadAsMultipartAsync(blobMultipartProvider);

                var retVal = new List<webModel.BlobInfo>();

                foreach (var blobInfo in blobMultipartProvider.BlobInfos)
                {
                    retVal.Add(new webModel.BlobInfo
                    {
                        Name = blobInfo.FileName,
                        Size = blobInfo.Size.ToString(),
                        MimeType = blobInfo.ContentType,
                        RelativeUrl = blobInfo.Key,
                        Url = _urlResolver.GetAbsoluteUrl(blobInfo.Key)
                    });
                }

                return Ok(retVal.ToArray());
            }
        }
Beispiel #5
0
        public IHttpActionResult UploadAssetFromUrl(string folder, [FromUri] string url)
        {
            using (var client = new WebClient())
            {
                var uploadInfo = new UploadStreamInfo
                {
                    FileByteStream = client.OpenRead(url),
                    FolderName     = folder,
                    FileName       = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url))
                };

                var key    = _blobProvider.Upload(uploadInfo);
                var retVal = new webModel.BlobInfo
                {
                    Name        = uploadInfo.FileName,
                    RelativeUrl = key,
                    Url         = _urlResolver.GetAbsoluteUrl(key)
                };
                return(Ok(retVal));
            }
        }
        public IHttpActionResult UploadAssetFromUrl(string folder, [FromUri]string url)
        {
            using (var client = new WebClient())
            {
                var uploadInfo = new UploadStreamInfo
                {
                    FileByteStream = client.OpenRead(url),
                    FolderName = folder,
                    FileName = HttpUtility.UrlDecode(System.IO.Path.GetFileName(url))
                };

                var key = _blobProvider.Upload(uploadInfo);
                var retVal = new webModel.BlobInfo
                {

                    Name = uploadInfo.FileName,
                    RelativeUrl = key,
                    Url = _urlResolver.GetAbsoluteUrl(key)
                };
                return Ok(retVal);
            }

        }