Inheritance: System.Net.Http.MultipartFormDataStreamProvider
示例#1
0
        private async Task <string> UploadAnyFileAsync(
            HttpContent httpContent,
            string[] allowedExtensions,
            string folderPath)
        {
            if (!httpContent.IsMimeMultipartContent("form-data"))
            {
                throw new NotSupportedException();
            }

            var provider = new CustomMultipartStreamProvider(folderPath);
            await httpContent.ReadAsMultipartAsync(provider);

            var fullFileName = provider.FileData.First().LocalFileName;
            var extension    = GetFileExtension(fullFileName);

            if (!allowedExtensions.Contains(extension))
            {
                await Task.Factory.StartNew(() => File.Delete(fullFileName));

                throw new InvalidDataException("Extension {0} is not allowed".FormatString(extension));
            }

            return(fullFileName);
        }
示例#2
0
        private async Task<string> UploadAnyFileAsync(
            HttpContent httpContent, 
            string[] allowedExtensions, 
            string folderPath)
        {
            if (!httpContent.IsMimeMultipartContent("form-data"))
            {
                throw new NotSupportedException();
            }

            var provider = new CustomMultipartStreamProvider(folderPath);
            await httpContent.ReadAsMultipartAsync(provider);
            var fullFileName = provider.FileData.First().LocalFileName;
            var extension = GetFileExtension(fullFileName);
            if (!allowedExtensions.Contains(extension))
            {
                await Task.Factory.StartNew(() => File.Delete(fullFileName));
                throw new InvalidDataException("Extension {0} is not allowed".FormatString(extension)); 
            }

            return fullFileName;
        }