c#文件流上传图片

1、第一步 定义参数

//主表TableA

public class TableA

{

public List<TableB> AccidentImgByte { get; set; }

}

//子表TableB

public class TableB{

public Byte[] AccidentImgflow { get; set; }//文件流

public String AccidentImgName { get; set; }//文件名称

public String AccidentImgExt { get; set; }//文件格式

}

2、接口

 public void AccidentImg_Request(TableA tModel){

 string VisitImgFile = string.Empty;//图片路径

if (bmModel.AccidentImgByte != null)
{
foreach (var item in tModel.AccidentImgByte)
{
VisitImgFile += AttachmentUrl(item.AccidentImgflow, item.AccidentImgName, item.AccidentImgExt) + ",";

}
if (!String.IsNullOrEmpty(VisitImgFile ))
{
VisitImgFile = VisitImgFile.Substring(0, VisitImgFile.Length - 1);
}
}

}

 

 

/// <summary>
/// 图片处理方法
/// </summary>
/// <param name="fileNameAttachment">文件流</param>
/// <param name="fileName">文件名称</param>
/// <param name="fileExt">文件格式</param>
/// <returns></returns>

private String AttachmentUrl(Byte[] fileNameAttachment, string fileName, string fileExt)
{
String aUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["web.config配置上传地址"];//服务器地址,用于返回服务器图片地址组合,如:http://x.x.x/
try
{
//写入目录
string attachmentPath = System.Web.Configuration.WebConfigurationManager.AppSettings["web.config配置上传地址"] + fileName + fileExt;
File.WriteAllBytes(attachmentPath, fileNameAttachment);
aUrl = aUrl + fileName + fileExt;//拼接返回地址 如:http://x.x.x/测试1.jpg
}
catch (Exception ex)
{
HX.BPM.Common.LogWriter.Write("日志", "BaggageMonitorAdd_Request", "行李监控流程发起处理异常", ex.Message);
aUrl = null;
}

return aUrl;
}

3、前端(外部)调用

private void button1_Click(object sender, EventArgs e)
{

 TableA ta=new TableA();

ta.AccidentImgByte = new AttModel[2];//定义长度

byte[] aImag = FileContent(@"本地图片路径");

ta.AccidentImgByte[0]=new TableB() {
AccidentImgflow = aImag,
AccidentImgName = "测试1",
AccidentImgExt=".jpg"
};

aImag = FileContent(@"本地图片路径");
ta.AccidentImgByte[1] = new TableB()
{
AccidentImgflow = aImag,
AccidentImgName = "测试2",
AccidentImgExt = ".jpg"
};

  var result = AccidentImg_Request(ta);

}

//文件转换成流

private static byte[] FileContent(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
try
{
byte[] buffur = new byte[fs.Length];
fs.Read(buffur, 0, (int)fs.Length);

return buffur;
}
catch (Exception ex)
{
return null;
}
finally
{
if (fs != null)
fs.Close();
}
}

转载于:https://www.cnblogs.com/chj929555796/p/7027842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值