代码之家  ›  专栏  ›  技术社区  ›  Ralf de Kleine

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

  •  0
  • Ralf de Kleine  · 技术社区  · 7 年前

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

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

    不知道我错过了什么。

    编辑:我也试过IIS和iisexpress。

    配置

    <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 回复  |  直到 5 年前
        1
  •  0
  •   Ralf de Kleine    7 年前

    @卡锡克拉曼帮我走上了正轨。

    我和邮递员用 binary 选项而不是 form-data 选项。 真管用!虽然我还是不明白为什么二进制选项 工作。

    [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

    推荐文章