Tôi gặp sự cố thực sự với api web của WCF.Lỗi với tệp tải lên WCF WEB API (Xem trước 6): Không thể ghi thêm byte vào bộ đệm so với kích thước bộ đệm tối đa được cấu hình: 65536
Tôi có một phương pháp đơn giản tải lên tệp và lưu vào đĩa. Tôi dường như đã thiết lập tất cả các params đúng, nhưng nhận được thông báo lỗi trên khi tôi cố gắng để tải lên một tập tin 2mb.
server Code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
HttpServiceHostFactory _factory = new HttpServiceHostFactory();
var config = new HttpConfiguration()
{
EnableTestClient = true,
IncludeExceptionDetail = true,
TransferMode = System.ServiceModel.TransferMode.Streamed,
MaxReceivedMessageSize = 4194304,
MaxBufferSize = 4194304
};
_factory.Configuration = config;
RouteTable.Routes.Add(new ServiceRoute("api/docmanage", _factory, typeof(WorksiteManagerApi)));
}
khách hàng:
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.MaxRequestContentBufferSize = 4194304;
var byteArray =
Encoding.ASCII.GetBytes(ConnectionSettings.WebUsername + ":" + ConnectionSettings.WebPassword);
HttpClient httpClient = new HttpClient(httpClientHandler);
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
httpClient.BaseAddress = new Uri(ConnectionSettings.WebApiBaseUrl);
httpClient.MaxResponseContentBufferSize = 4194304;
...
multipartFormDataContent.Add(new FormUrlEncodedContent(formValues));
multipartFormDataContent.Add(byteArrayContent);
var postTask = httpClient.PostAsync("api/docmanage/UploadFile", multipartFormDataContent);
Sau đó, trên máy chủ:
[WebInvoke(Method = "POST")]
public HttpResponseMessage UploadFile(HttpRequestMessage request)
{
// Verify that this is an HTML Form file upload request
if (!request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
// Create a stream provider for setting up output streams
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider();
// Read the MIME multipart content using the stream provider we just created.
IEnumerable<HttpContent> bodyparts = request.Content.ReadAsMultipart(streamProvider);
foreach (var part in bodyparts)
{
switch (part.Headers.ContentType.MediaType)
{
case "application/octet-stream":
if (part.Headers.ContentLength.HasValue)
{
// BLOWS UP HERE:
var byteArray = part.ReadAsByteArrayAsync().Result;
if (null == fileName)
{
throw new HttpResponseException(HttpStatusCode.NotAcceptable);
}
else
{
uniqueFileId = Guid.NewGuid().ToString();
string tempFilename = Path.GetTempPath() + @"\" + uniqueFileId + fileName;
using (FileStream fileStream = File.Create(tempFilename, byteArray.Length))
{
fileStream.Write(byteArray, 0, byteArray.Length);
}
}
}
break;
}
}
}
Bất kỳ ý tưởng? Tôi đang sử dụng xem trước mới nhất của api web .... Tôi nhận thấy rất nhiều là mất tích từ các tài liệu hỗ trợ nhưng có vẻ như có một số giới hạn bộ đệm mà tôi không thể tìm hiểu làm thế nào để xác định hoặc đang bị bỏ qua .... .
Khi đăng mã, hãy đảm bảo mã được định dạng để có thể đọc được càng tốt. Đó là thô lỗ để yêu cầu mọi người dành thời gian của họ chọn thông qua một mẫu được định dạng sai. –
Bạn có bất kỳ mục nhập cấu hình nào trên máy chủ và ứng dụng khách không? Nếu vậy, bạn có thể gửi chúng quá – Rajesh
để lại tất cả cấu hình cho mã chỉ ... bạn có thể xem thông số cấu hình được chuyển đến dịch vụ khi tạo (và trên máy khách) .... –