如何使用C#将byte []转换为HttpPostedFileBase [英] how to convert a byte[] to HttpPostedFileBase using c#

查看:175
本文介绍了如何使用C#将byte []转换为HttpPostedFileBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#将byte []转换为HttpPostedFileBase.在这里,我尝试了以下方式.

how to convert byte[] into HttpPostedFileBase using c#. here i have tried the following way.

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)bytes;

我遇到了一个无法隐式转换的错误.

I am getting an cannot implicitly convert error.

推荐答案

如何创建自定义发布文件? :)

What about creating a custom postedfile? :)

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; }
}

您可以像这样简单地使用:

That you can simply use like this:

byte[] bytes = System.IO.File.ReadAllBytes(localPath);
HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(bytes);

这篇关于如何使用C#将byte []转换为HttpPostedFileBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆