Replies: 13 comments
-
Can only return primitive types,int,string,number。 |
Beta Was this translation helpful? Give feedback.
-
In net4.7 or net4.8, there are no such restrictions |
Beta Was this translation helpful? Give feedback.
-
I'm sorry I didn't read it carefully. I tried to return object before, but all failed. You have given me confidence here. OS: |
Beta Was this translation helpful? Give feedback.
-
Can I call Stream.Write(byte[] buffer, int offset, int count) in javaScript? |
Beta Was this translation helpful? Give feedback.
-
I don't think it will work to use Also, byte[] seems to have the problem of transformation failure, you can try to use string instead. js var fileStream = await WV_Stream.NewFileStream("D:\\output.jpg");
var bytes = await WV_Stream.GetBytes("D:\\input.jpg");
await WV_Stream.WriteStream(fileStream, JSON.stringify(bytes), 0, bytes.length);
fileStream.Dispose(); Form1.cs using Microsoft.Web.WebView2.WinForms;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WV_netToJs {
public partial class Form1 : Form {
public WebView2 wv2;
public Form1() {
InitializeComponent();
Init();
}
public async void Init() {
wv2 = new WebView2();
this.Controls.Add(wv2);
wv2.Dock = DockStyle.Fill;
await wv2.EnsureCoreWebView2Async();
wv2.CoreWebView2.AddHostObjectToScript("WV_Stream", new WV_Stream());
string path = @"C:\Users\u1\Desktop\netToJs.html";
wv2.Source = new Uri(path);
}
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class WV_Stream {
public FileStream NewFileStream(string path) {
FileStream fs = File.Create(path) ;
return fs;
}
public byte[] GetBytes(string path) {
byte[] result;
using (FileStream fs = File.OpenRead(path)) {
result = new byte[fs.Length];
fs.Read(result, 0, (int)fs.Length);
}
return result;
}
public void WriteStream(Stream fs, string buffer, int offset, int count) {
fs.Write(StringToBytes(buffer), offset, count);
}
private byte[] StringToBytes(string data) {
string[] splitData = data.Trim(new char[] { '[', ']' }).Split(',');
byte[] result = new byte[splitData.Length];
for (int i = 0; i < splitData.Length; i++) {
result[i] = byte.Parse(splitData[i]);
}
return result;
}
}
} Of course, the prerequisite for doing this is to be able to pass C# objects to js. |
Beta Was this translation helpful? Give feedback.
-
Hi @hbl917070, thanks for your feedback! |
Beta Was this translation helpful? Give feedback.
-
Hi @hbl917070 ,
As a workaround, you can wrap the System.IO.Stream:
And use the
|
Beta Was this translation helpful? Give feedback.
-
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class testType
{
public void test(Object param)
{
Console.WriteLine(param);
}
} How do you get an object that javascript passes in? C# will get a System.__ComObject type parameter, this type I looked up a lot of information, can not get the key and value inside. Can you help me |
Beta Was this translation helpful? Give feedback.
-
In my tests, I passed Uint8Array and ArrayBuffer data. __ComObject in C#, which is the closest type to byte data The only good news is that you can pass Array[int]. In C# it can keep an array state |
Beta Was this translation helpful? Give feedback.
-
Hi @yunate , The solution of enclosing the stream in another class can work smoothly. Since this solution works, is it possible to use the Inheritance solution? #pragma warning disable CS0618
[ClassInterface(ClassInterfaceType.AutoDual)]
#pragma warning restore CS0618
[ComVisible(true)]
public class WV_Form {
public MyForm NewForm() {
var w = new MyForm();
w.Show();
return w;
}
} MyForm #pragma warning disable CS0618
[ClassInterface(ClassInterfaceType.AutoDual)]
#pragma warning restore CS0618
[ComVisible(true)]
public class MyForm : Form {
} |
Beta Was this translation helpful? Give feedback.
-
@hbl917070, |
Beta Was this translation helpful? Give feedback.
-
Hi @DebugCodeBody, |
Beta Was this translation helpful? Give feedback.
-
I already know what to do, thank you for your reply |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is a simple test
Get a Stream Object from C# using js, save it as a js variable
Then send this js variable (C# Object) back to C#
netToJs.html
Form1.cs
This code works fine in net4.7 or net4.8,
But in net6 or net7,
await WV_Stream.GetStream("D:\\a.jpg")
returnsnull
I'm not sure if this is a limitation due to security concerns or simply a bug.
OS:
Windows 10 19045.2965
WebView2 version:
1.0.1774.30
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50
Beta Was this translation helpful? Give feedback.
All reactions