You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.7 KiB
65 lines
2.7 KiB
using System.Data.SqlClient;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
|
|
namespace DcmToPng.Helper
|
|
{
|
|
public static class UploadHelper
|
|
{
|
|
|
|
public static void Upload()
|
|
{
|
|
var time = DateTime.Now;
|
|
// 获取文件夹下所有文件的路径
|
|
var files = Directory.GetFiles(Constant.ImgPath);
|
|
var i = 0;
|
|
// 建立数据库连接
|
|
using (var connection = new SqlConnection(Constant.ConnectionString))
|
|
{
|
|
// 遍历文件路径并输出
|
|
foreach (var filePath in files)
|
|
{
|
|
try
|
|
{
|
|
var fileName = Path.GetFileName(filePath);
|
|
var eid = Convert.ToInt64(fileName.Split('-')[0]);
|
|
var reportNo = fileName.Split('-')[1];
|
|
|
|
using (Image image = Image.FromFile(filePath))
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
image.Save(memoryStream, ImageFormat.Png);
|
|
var imgBytes = memoryStream.ToArray();
|
|
//上传
|
|
{
|
|
connection.Open();
|
|
// 创建插入记录的 SQL 查询
|
|
// 创建命令对象
|
|
using (var command = new SqlCommand(Constant.SqlInsert, connection))
|
|
{
|
|
// 设置参数值
|
|
command.Parameters.AddWithValue("@ImageData", imgBytes);
|
|
command.Parameters.AddWithValue("@EID", eid);
|
|
command.Parameters.AddWithValue("@ReportNo", reportNo);
|
|
command.Parameters.AddWithValue("@InTime", time);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
connection.Close();
|
|
}
|
|
}
|
|
}
|
|
i++;
|
|
// 删除上传成功的文件
|
|
File.Delete(filePath);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine ("[Upload][Error]" + Path.GetFileName(filePath) + " | " + e.Message);
|
|
}
|
|
}
|
|
}
|
|
Console.WriteLine("[图片上传成功]" + i);
|
|
}
|
|
}
|
|
} |