将大于64KB的文件发布到WebApi

时间:2018-10-26 16:33:05

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

我正在使用Postman将大于64KB的二进制文件发布到WebApi控制器。

读取流时,我收到前64KB数据,所有较大的数据都填充为空字节。

不知道我错过了什么。

编辑:我也尝试过IIS和IIS Express。

配置

   
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="DocumentService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="2097152" />
  </system.web>

  <system.webServer>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <remove name="WebDAV" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
  <applicationSettings>
    <DocumentService.Properties.Settings>
      <setting name="datafolder" serializeAs="String">
        <value>c:\temp\</value>
      </setting>
    </DocumentService.Properties.Settings>
  </applicationSettings>
</configuration>

代码

[Route("api/documents/{company}/{type}/{id}")]
public async Task<IHttpActionResult> Post(string company, string type, string id)
{

    string path = System.IO.Path.Combine(Properties.Settings.Default.datafolder, company, type, id);
    if (System.IO.File.Exists(path))
    {
        return BadRequest($"File {path} already exists");
    }

    var data = Request.Content.ReadAsStreamAsync().Result;
    data.Position = 0;
    var buffer = new Byte[data.Length];
    // <-- data.length corresponds with the length of the file. 
    //     the first 64KB are filled with data the rest are empty bytes
    await data.ReadAsync(buffer, 0, (int)data.Length);
    System.IO.File.WriteAllBytes(path, buffer);

    return Ok();
}

1 个答案:

答案 0 :(得分:0)

@kaarthickraman帮助我步入正轨。

我一直在使用binary选项而不是form-data选项与Postman进行测试。 这可行!尽管我仍然不明白为什么二进制选项不起作用

还进行了代码更改:

[Route("api/documents/{company}/{type}/{id}")]
public async Task<IHttpActionResult> Post(string company, string type, string id)
{

    string path = System.IO.Path.Combine(Properties.Settings.Default.datafolder, company, type, id);
    if (System.IO.File.Exists(path))
    {
            return BadRequest($"File {path} already exists");
    }

    HttpRequest req = HttpContext.Current.Request;
    if (req.Files.Count != 1)
    {
        return BadRequest();
    }

    HttpPostedFile file = req.Files[0];
    byte[] buffer = new Byte[file.InputStream.Length];
    await file.InputStream.ReadAsync(buffer, 0, (int)file.InputStream.Length);
    File.WriteAllBytes(path, buffer);

    return Ok();
}

postman