繁体   English   中英

我如何在 C# 中将字节数组转换为 HttpPostedFileBase 图像文件?

[英]How i can convert byte array into HttpPostedFileBase image file in c#?

我正在尝试将字节数组转换为 C# 中用于 Web API 的 HttpPostedFileBase 文件。 但是转换后,我找不到文件名。

public class MemoryPostedFile : HttpPostedFileBase
{
    private readonly byte[] fileBytes;

    public MemoryPostedFile(byte[] fileBytes, string fileName = null)
    {
        this.fileBytes = fileBytes;
        this.FileName = fileName;
        this.InputStream = new MemoryStream(fileBytes);
    }

    public override int ContentLength => fileBytes.Length;

    public override string FileName { get; }

    public override Stream InputStream { get; }
}

HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(model.UserImage);

对您的代码的一个观察是您没有将文件名传递给MemoryPostedFile构造函数。 尝试将objFile更改为以下内容:

HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(model.UserImage, model.FileName);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM