我在C#中看到过有关此主题的多个已解决的问题,但我无法使其正常工作。这是我到目前为止所做的事情
var imageStream = new MemoryStream();
using (imageStream)
{
bp.Save(@"C:\Users\denskie\Desktop\Capture.jpg", ImageFormat.Jpeg);
imageStream.Position = 0;
byte[] imageBytes = imageStream.ToArray();
var wc = new WebClient();
//wc.UploadData("http://localhost:3000/file", "PUT", imageBytes);
wc.UploadFile("http://localhost:3000/file", "PUT", @"D:\Capture.jpg");
}
但这是一个错误说:
另一方面,我有一个javascript代码将文件(图像)发送到我的服务器:
var el = document.querySelector('input[type="file"]');
el.addEventListener('change', function(e) {
let files = e.target.files;
for (let i = 0; i < files.length; i++) {
let file = files[i];
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'http://localhost:3000/file', true);
xhr.onload = (event) => {
console.log('done uploading!');
};
xhr.upload.onprogress = (event) => {
let percent = 100 * (event.loaded / event.total);
console.log(percent+'% uploaded');
};
xhr.send(file);
}
});
它的工作非常好。我想在c#
中做这样的事情