using CefSharp.WinForms;
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
namespace Gs.DevApp.UserControl
{
public partial class ShowFile : DevExpress.XtraEditors.XtraForm
{
string strConn = "Data Source=192.168.1.146,12468;Initial Catalog=GS_MES;User ID=mesUser;Password =qixi1qaz@WSXmes";
private static readonly string WebApiUrl =
ConfigurationManager.AppSettings["WebApiUrl"];
private ChromiumWebBrowser chromeBrowser;
string urlPath;
public ShowFile(string _urlPath)
{
InitializeComponent();
this.urlPath = _urlPath;
this.Load += ShowFile_Load;
this.FormClosed += ShowFile_FormClosed;
}
private void ShowFile_FormClosed(object sender, FormClosedEventArgs e)
{
chromeBrowser.Dispose();
}
private void ShowFile_Load(object sender, EventArgs e)
{
string fileType = GetFileType(urlPath);
string filePath = "";
//这是guid,不是路径,根据guid 读取到它的路径
if (urlPath.Length == 36)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(" select top 1 url_Path from [dbo].[MES_FILE] where parent_Guid='" + urlPath + "'");
object obj = GetSingle(sb.ToString());
if (obj != null)
{
filePath = System.Uri.EscapeUriString(obj.ToString());
fileType = GetFileType(filePath);
}
else
filePath = System.Uri.EscapeUriString(urlPath);
}
else
filePath = System.Uri.EscapeUriString(urlPath);
string _url = WebApiUrl + "upload/h5/site.html?fileType=" + fileType + "&filePath=" + filePath + "&mid=" + Guid.NewGuid().ToString();
chromeBrowser = new ChromiumWebBrowser(_url);
this.pane1.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
txtUrl.Text = _url;
txtUrl.ReadOnly = true;
}
public static string GetFileType(string fileName)
{
// 获取文件扩展名
string extension = Path.GetExtension(fileName);
// 根据扩展名判断文件类型
switch (extension.ToLowerInvariant())
{
case ".jpg":
case ".jpeg":
case ".png":
return "img";
case ".pdf":
return "pdf";
case ".mp4":
return "mp4";
default:
return "other";
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
/// 查询结果(object)
private object GetSingle(string SQLString)
{
using (var connection = new SqlConnection(strConn))
{
using (var cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
var obj = cmd.ExecuteScalar();
if (Equals(obj, null) || Equals(obj, DBNull.Value))
return null;
return obj;
}
catch (SqlException e)
{
connection.Close();
throw new Exception(e.Message);
}
}
}
}
}
}