public Task<IEnumerable<FileDesc>> Post()
        {
            const string nomeDaPasta = "uploads";
            var path = HttpContext.Current.Server.MapPath("~/" + nomeDaPasta);
            var rootUrl = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, String.Empty);

            if (Request.Content.IsMimeMultipartContent())
            {
                var streamProvider = new CustomMultipartFormDataStreamProvider(path);
                var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
                {

                    if (t.IsFaulted || t.IsCanceled)
                        throw new HttpResponseException(HttpStatusCode.InternalServerError);

                    var fileInfo = streamProvider.FileData.Select(i =>
                    {
                        var info = new FileInfo(i.LocalFileName);
                        return new FileDesc(info.Name, rootUrl + "/" + nomeDaPasta + "/" + info.Name, info.Length / 1024);
                    });
                    return fileInfo;
                });

                return task;
            }

            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "A requisição não esta bem formatada"));
        }
        //HttpResponseMessage
        public Task<IEnumerable<FileDesc>> Post()
        {

            const string folderName = "uploads";

            //  Configures the path so save the uploaded file, while also accessing the HTTPContext which includes information about the original file path.

            string uploadFolderPath = HttpContext.Current.Server.MapPath(string.Format("~/{0}", folderName));

            //  Ensure the folder exists or the code will fail6
            if (!Directory.Exists(uploadFolderPath))
            {
                Directory.CreateDirectory(uploadFolderPath);
            }

            string rootUrl = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, String.Empty);

            if (Request.Content.IsMimeMultipartContent())
            {
                //  Basically there are some extra characters in the file path in the HTTP headers, so we need to removed those.
                var streamProvider = new CustomMultipartFormDataStreamProvider(uploadFolderPath);

                var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith<IEnumerable<FileDesc>>(t =>
                {

                    if (t.IsFaulted || t.IsCanceled)
                    {
                        throw new HttpResponseException(HttpStatusCode.InternalServerError);
                    }

                    var fileInfo = streamProvider.FileData.Select(i =>
                    {
                        var info = new FileInfo(i.LocalFileName);
                        return new FileDesc(info.Name, String.Format("{0}/{1}/{2}", rootUrl, folderName, info.Name), info.Length / 1024);
                    });
                    return fileInfo;
                });

                //  Results of all the files uploaded are returned as an ienumerable<filedescription>
                //  It would be interesting to know how this gets converted to Json for KnockoutJS.
                return task;
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }

        }
예제 #3
0
        public async Task<HttpResponseMessage> Post()
        {
            string folderName = "uploads";
            string PATH = HttpContext.Current.Server.MapPath("~/" + folderName);
            string rootUrl = Request.RequestUri.AbsoluteUri.Replace(Request.RequestUri.AbsolutePath, String.Empty);
            if (Request.Content.IsMimeMultipartContent())
            {
                var streamProvider = new CustomMultipartFormDataStreamProvider(PATH);
                //IEnumerable<FileDesc> fileInfos = null;
                await Request.Content.ReadAsMultipartAsync(streamProvider);
                var fileInfo = streamProvider.FileData.Select(i =>
                   {
                       var info = new FileInfo(i.LocalFileName);
                       return new FileDesc(info.Name, rootUrl + "/" + folderName + "/" + info.Name, info.Length / 1024);
                   });

                return Request.CreateResponse(HttpStatusCode.OK, fileInfo);
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
            }
        }