Coder Social home page Coder Social logo

Comments (10)

li-zheng-hao avatar li-zheng-hao commented on July 19, 2024
public static async Task SaveToFileAsync(this Stream stream,string filename)
{
    await using FileStream destination = new FileStream(filename, FileMode.Create, FileAccess.Write);
    await new BufferedStream(stream, 1048576).CopyToAsync((Stream) destination);
}

测试了一下换成这种写法也不会报错

from masuit.tools.

ldqk avatar ldqk commented on July 19, 2024

能否提供一个完整的可独立运行可复现这个bug的demo,我测试是可以正常调用不报错的,这是我的demo代码:
ConsoleApp1.zip

from masuit.tools.

li-zheng-hao avatar li-zheng-hao commented on July 19, 2024

我写了几个测试用例也没能复现,我再尝试一下看看

from masuit.tools.

li-zheng-hao avatar li-zheng-hao commented on July 19, 2024

复现了,下面是测试用例:

using Flurl.Http;
using Masuit.Tools.Files;
using Xunit.Abstractions;
[Fact]
public async Task 测试BUG()
{
    for (int i = 0; i < 5; i++)
    {
        var stream=await "https://api.tianditu.gov.cn/cdn/plugins/cesium/Cesium_ext_min.js"
            .GetStreamAsync();
        await stream.SaveFileAsync("test-err" + Guid.NewGuid().ToString());
    }
}

image

用到的库:

  1. Masuit.Tools.Abstractions 2.6.7.4
  2. Flurl.Http 3.2.4

from masuit.tools.

li-zheng-hao avatar li-zheng-hao commented on July 19, 2024

修改版本测试用例通过:

[Fact]
public async Task 修复后()
{
    for (int i = 0; i < 5; i++)
    {
        var stream=await "https://api.tianditu.gov.cn/cdn/plugins/cesium/Cesium_ext_min.js"
            .GetStreamAsync();
        // await stream.SaveFileAsync("test-err" + Guid.NewGuid().ToString());
        await FixedSaveToFileAsync(stream, "test-err" + Guid.NewGuid().ToString());
    }
}

static async Task FixedSaveToFileAsync(Stream stream,string filename)
{
    await using FileStream destination = new FileStream(filename, FileMode.Create, FileAccess.Write);
    await new BufferedStream(stream, 1048576).CopyToAsync((Stream) destination);
}

from masuit.tools.

ldqk avatar ldqk commented on July 19, 2024

这个是用法问题了,Http响应流不能直接拿来存文件流,需要先转MemoryStream再用

from masuit.tools.

li-zheng-hao avatar li-zheng-hao commented on July 19, 2024

这个是用法问题了,Http响应流不能直接拿来存文件流,需要先转MemoryStream再用

如果是这样的话,扩展方法的this应该用MemoryStream类型更好吧,不然stream的变量点一下之后就出来了这个扩展方法,很容易写出bug诶

from masuit.tools.

ldqk avatar ldqk commented on July 19, 2024

Stream体系本来就很混乱,各种各样的实现,但是又不能限定只能给MemoryStream做扩展,很多其他的流类型也是可以正常转储文件的

from masuit.tools.

ldqk avatar ldqk commented on July 19, 2024

遇到不能直接转储文件的Stream就借助MemoryStream中转一下咯

from masuit.tools.

li-zheng-hao avatar li-zheng-hao commented on July 19, 2024

遇到不能直接转储文件的Stream就借助MemoryStream中转一下咯

那也只能这样了 ...🤣

from masuit.tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.