日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#文件流上传图片

發布時間:2024/6/21 C# 76 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的c#文件流上传图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。