I ran into a problem that when downloading a file larger than 100 kb, the api web controller will not be called. I searched for a while and found that maybe I need to set maxRequestLength in webconfig. But how to install it in your own host?
customer
public async Task<bool> Upload(DeviceFile file,string path) { var formData = new MultipartFormDataContent(); var request = new HttpRequestMessage(); var md5 = new MD5CryptoServiceProvider(); var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read,bufferSize:4096,useAsync:true); fileStream.Position = 0; var hash = md5.ComputeHash(fileStream); fileStream.Position = 0; formData.Add( new StreamContent(fileStream), file.Name, file.Name, new { Info = file.Info, } ); request.Method = HttpMethod.Post; request.Content = formData; request.RequestUri = new Uri(client.BaseAddress,"api/file/"); try { var response = await client.SendAsync(request).ConfigureAwait(false); await response.Content.ReadAsAsync<bool>().ConfigureAwait(false); } catch (Exception ex) { this.logger.Log(ex.ToString(), Category.Info, Priority.None); } return true; }
server controller
public async Task<HttpResponseMessage> Add() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new MultipartFormDataStreamProvider("D:/"); try { await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the file names. foreach (MultipartFileData file in provider.FileData) { Console.WriteLine(file.Headers.ContentDisposition.FileName); Console.WriteLine("Server file path: " + file.LocalFileName); } foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { Console.WriteLine(string.Format("{0}: {1}", key, val)); } } return Request.CreateResponse(HttpStatusCode.OK); } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } }
source share