1、 为了使网页能够与winform交互 将com的可访问性设置为真
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
2、在webBrowser控件中设置可被html页面调用的类即:webBrowser1.ObjectForScripting = this;前端即可通过window.external访问this对象
3、html页面调用后台方法:window.external.方法名(); 此处的window.external相当于webBrowser1.ObjectForScripting
设计:
后台代码:
注意:视频地址需要修改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WebKit;
using System.IO;
using DXApplication1.Helper;
namespace DXApplication1
{
//为了使网页能够与winform交互 将com的可访问性设置为真
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class webBrowser : DevExpress.XtraEditors.XtraForm
{
public webBrowser()
{
InitializeComponent();
}
private void webBrowser_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;//消除脚本错误
webBrowser1.Navigate(@"C:\source\repos\DXApplication1\DXApplication1\Html\HTMLPage2.html");
webBrowser1.ObjectForScripting = this;
}
public string FileLode(string url)
{
//返回路径
string result = url;
if (!string.IsNullOrEmpty(url))
{
//保存文件名称
string saveName = Guid.NewGuid().ToString() + ".mp4";
string basePath = "UploadFile";
string saveDir = DateTime.Now.ToString("yyyy-MM-dd");
//获取应用程序的当前工作目录
string MapPath = Directory.GetCurrentDirectory();
// 文件上传后的保存路径
string serverDir = Path.Combine(MapPath, basePath, saveDir);
//保存文件完整路径
string fileNme = Path.Combine(serverDir, saveName);
//项目中是否存在文件夹,不存在创建