我们如何将文件上传到服务器?

时间:2016-12-25 17:12:16

标签: asp.net-web-api c#-6.0

这是我的api

[HttpPost]        
[Route("uploadfile")]

    public HttpResponseMessage PostFile(HttpRequest request)
    {
        HttpResponseMessage result = null;
        var httpRequest = request;//HttpContext.Current.Request;
        if (httpRequest.Files.Count > 0)
        {
            var docfiles = new List<string>();
            foreach (string file in httpRequest.Files)
            {
                var postedFile = httpRequest.Files[file];
                var filePath = HttpContext.Current.Server.MapPath("Uploads/" + postedFile.FileName);
                postedFile.SaveAs(filePath);

                docfiles.Add(filePath);
            }
            result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
        }
        else
        {
            result = Request.CreateResponse(HttpStatusCode.BadRequest);
        }
        return result;
    }

这是我的客户代码

  private async void button_Click(object sender, RoutedEventArgs e)
   {
   byte[] a = File.ReadAllBytes(@"C:\My folder\MyFile.txt");
        FileStream stream = File.Open(@"C:\My folder\MyFile.txt", FileMode.Open);

        Upload(@"http://localhost:56885/uploadfile", "MyFile", stream, a);

        MessageBox.Show("File uploaded Successfully");
   }

   private Stream Upload(string url, string param1, 
   Stream fileStream, byte[] fileBytes)        
   {
        HttpContent stringContent = new StringContent(param1);
        HttpContent fileStreamContent = new StreamContent(fileStream);
        HttpContent bytesContent = new ByteArrayContent(fileBytes);
        using (var client = new HttpClient())
        using (var formData = new MultipartFormDataContent())
        {
            formData.Add(stringContent, "param1", "param1");
            formData.Add(fileStreamContent, "file1", "file1");
            formData.Add(bytesContent, "file2", "file2");

            var response = client.PostAsync(url, formData).Result;

            if (!response.IsSuccessStatusCode)
            {
                return null;

            }

            MessageBox.Show("File uploaded Successfully");
            return response.Content.ReadAsStreamAsync().Result;
        }

    }

我收到了错误{ “message”:“此资源不支持请求实体的媒体类型'multipart / form-data'。”, “exceptionMessage”:“没有MediaTypeFormatter可用于从媒体类型为'multipart / form-data'的内容中读取'HttpRequest'类型的对象。”, “exceptionType”:“System.Net.Http.UnsupportedMediaTypeException”, “stackTrace”:“在System.Web.Http.ModelBinding.FormatterParameterBinding的System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型类型,IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable 1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken) .ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)“ }

0 个答案:

没有答案