How do I implement a web API controller to receive uploaded uploads using a jQuery file upload?

As the title says, I need help implementing a web API controller to accept batch uploads using jQuery file upload. Any help (including links to existing articles / tutorials) would be much appreciated.

+3


source to share


2 answers


Start on the client side first. You must set the maxChunkSize parameter for batch uploads. After that, you need a unique ID for each file to identify each chunk on the server and add the corresponding chunk data to the correct file.

$('#fileupload')
        .bind('fileuploadsubmit', function (e, data) {
            data.headers = $.extend(data.headers,
                {"X-File-Identifier": generateFileUniqueIdentifier(data)})
            });
        });

generateFileUniqueIdentifier = function(data){
    var file=data.files[0],
    var result = file.relativePath||file.webkitRelativePath||file.fileName||file.name;

    return result.replace(/[^0-9a-zA-Z_-]/img,"") + "-" + i.size + "-" + $.now()
} 

      

Now server side: ApiController

 public class UploadController : ApiController
 {
        [HttpPost]
        [Route("upload/{targetFolder:int}")]
        [ValidateMimeMultipartContentFilter]
        public async Task<IHttpActionResult> UploadDocument(int targetFolder)
        {
            var uploadFileService = new UploadFileService();
            UploadProcessingResult uploadResult = await uploadFileService.HandleRequest(Request);

            if (uploadResult.IsComplete)
            {
                // do other stuff here after file upload complete    
                return Ok();
            }

            return Ok(HttpStatusCode.Continue);

        }
}

      

The service class that actually loads the file. These are auxiliary fragments or a whole file.



public class UploadFileService
{
        private readonly string _uploadPath;
        private readonly MultipartFormDataStreamProvider _streamProvider;

        public UploadFileService()
        {
            _uploadPath = UserLocalPath;
            _streamProvider = new MultipartFormDataStreamProvider(_uploadPath);
        }

        #region Interface

        public async Task<UploadProcessingResult> HandleRequest(HttpRequestMessage request)
        {
            await request.Content.ReadAsMultipartAsync(_streamProvider);
            return await ProcessFile(request);
        }

        #endregion    

        #region Private implementation

        private async Task<UploadProcessingResult> ProcessFile(HttpRequestMessage request)
        {
            if (request.IsChunkUpload())
            {
                return await ProcessChunk(request);
            }

            return new UploadProcessingResult()
            {
                IsComplete = true,
                FileName = OriginalFileName,
                LocalFilePath = LocalFileName,
                FileMetadata = _streamProvider.FormData
            };
        }

        private async Task<UploadProcessingResult> ProcessChunk(HttpRequestMessage request)
        {
            //use the unique identifier sent from client to identify the file
            FileChunkMetaData chunkMetaData = request.GetChunkMetaData();
            string filePath = Path.Combine(_uploadPath, string.Format("{0}.temp", chunkMetaData.ChunkIdentifier));

            //append chunks to construct original file
            using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate | FileMode.Append))
            {
                var localFileInfo = new FileInfo(LocalFileName);
                var localFileStream = localFileInfo.OpenRead();

                await localFileStream.CopyToAsync(fileStream);
                await fileStream.FlushAsync();

                fileStream.Close();
                localFileStream.Close();

                //delete chunk
                localFileInfo.Delete();
            }

            return new UploadProcessingResult()
            {
                IsComplete = chunkMetaData.IsLastChunk,
                FileName = OriginalFileName,
                LocalFilePath = chunkMetaData.IsLastChunk ? filePath : null,
                FileMetadata = _streamProvider.FormData
            };

        }

        #endregion    

        #region Properties

        private string LocalFileName
        {
            get
            {
                MultipartFileData fileData = _streamProvider.FileData.FirstOrDefault();
                return fileData.LocalFileName;
            }
        }

        private string OriginalFileName
        {
            get
            {
                MultipartFileData fileData = _streamProvider.FileData.FirstOrDefault();
                return fileData.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
            }
        }

        private string UserLocalPath
        {
            get
            {
               //return the path where you want to upload the file                   
            }
        }

        #endregion    
    }

      

Extensions over HttpRequestMessagge are used to identify a chunk request

public static class HttpRequestMessageExtensions
{
        public static bool IsChunkUpload(this HttpRequestMessage request)
        {
            return request.Content.Headers.ContentRange != null;
        }

        public static FileChunkMetaData GetChunkMetaData(this HttpRequestMessage request)
        {
            return new FileChunkMetaData()
            {
                ChunkIdentifier = request.Headers.Contains("X-DS-Identifier") ? request.Headers.GetValues("X-File-Identifier").FirstOrDefault() : null,
                ChunkStart = request.Content.Headers.ContentRange.From,
                ChunkEnd = request.Content.Headers.ContentRange.To,
                TotalLength = request.Content.Headers.ContentRange.Length
            };
        }
    }

      

And at the end the service response model and chunk metadata

public class FileChunkMetaData
{
    public string ChunkIdentifier { get; set; }

    public long? ChunkStart { get; set; }

    public long? ChunkEnd { get; set; }

    public long? TotalLength { get; set; }

    public bool IsLastChunk
    {
        get { return ChunkEnd + 1 >= TotalLength; }
    }
}

public class UploadProcessingResult
{
    public bool IsComplete { get; set; }

    public string FileName { get; set; }

    public string LocalFilePath { get; set; }

    public NameValueCollection FileMetadata { get; set; }
}

      

+12


source


You can find some inspiration here: Uploading Multiple ASP.NET Files with a Drag and Drop Bar using HTML5 . An example of a method for dispatching a loaded channel starts with UploadFile

. On the client, the jquery file upload option maxChunkSize

should be set according to https://github.com/blueimp/jQuery-File-Upload/wiki/Options .



0


source







All Articles