Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用c#实现http服务器接受ajax请求,c#实现http客户端上传文件到远程服务器 #32

Open
abeet opened this issue Apr 16, 2018 · 0 comments

Comments

@abeet
Copy link
Owner

abeet commented Apr 16, 2018

目的

在ZCMS中的文章编辑器是UEditor,我曾经在写了一个UEditor插件叫uploadLocalImage,作用是自动上传粘贴到文章编辑器中的本地图片到服务器。在从word文档中copy内容到编辑器中就可以自动上传其中的图片了。
在之前的笔记 #16 中有记录,在chrome插件中是通过NPAPI调用本地dll文件去读取本地文件,chrome45及以后的版本,加强了安全限制,禁止了NPAPI,所以chrome插件不能再访问本地文件了。
现在的思路是要求客户安装一个可执行程序,这个程序在本地启一个httpserver,编辑器中用js通过ajax和本地的httpserver通讯,通知这个本地服务上传本地硬盘上的文件到cms后台去。从而解决js不能访问本地文件的限制。而且这种方式与浏览器无关,不用为不同的浏览器开发插件了。

实现过程

services.AddCors (options => options.AddPolicy ("AllowAll", policy => policy.AllowAnyHeader ().AllowAnyMethod ().AllowAnyOrigin ().AllowCredentials ()));

经测试可跨域访问

public IActionResult status (string name) {
    var jsonObj = new {
        version = "1.0.0",
        status = "ok",
        error = "",
    };
    return Json (jsonObj);
}

public object status (string name) {
    var jsonObj = new {
        version = "1.0.0",
        status = "ok",
        error = "",
    };
    return jsonObj;
}
$.ajax({
	url: 'http://localhost:10808/localupload',
	type: 'POST',
	contentType: 'application/json',
	data: JSON.stringify({
		"action":"localupload",
		"param1":"file:///C:\\Users\\abeet\\AppData\\Local\\Temp\\ksohtml\\wps58D8.tmp.jpg",
		"sendUrl":"http://localhost/api/catalogresources/9191/frontupload?siteID=14",
		"localSize":1024,
		"localExt":"gif,jpg,bmp,svg,png",
		"cookie":"IPCity=%E6%9C%AA%E7%9F%A5; ASPSESSIONIDQQARATRD=AEHBAMACAEIMHEHGPOGLDGEO"
	})
 }).then(function(res){
	console.log(res)
 },function(res){
	console.error(res)
 })
{
"action":"localupload",
"param1":"file:///C:\\Users\\abeet\\AppData\\Local\\Temp\\ksohtml\\wps7D7A.tmp.jpg",
"sendUrl":"http://localhost/api/catalogresources/9191/frontupload?siteID=14",
"localSize":1024,
"localExt":"gif,jpg,bmp,svg,png",
"cookie":"IPCity=%E6%9C%AA%E7%9F%A5; ASPSESSIONIDQQARATRD=AEHBAMACAEIMHEHGPOGLDGEO"
}

结果有错误,在java类里打断点跟踪后发现,其一要传递siteID和addUser,其二我们后台java方法限定了表单元素的name为file,调整后,顺利地通过c#上传了临时文件夹下的图片。

  • 至此终于可以在cms后台的编辑器里正式测试了。
  • 在js里对预览路径进行处理后就ok了
  • 生成exe文件,在项目根目录执行 dotnet build -r win10-x64
  • 然后是要注册为服务,在百度里搜索“C# 注册为windows服务”找到的文章有
    https://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html
    需要特别的写法,还需要使用installutil.exe 去安装,这个安装文件客户的机器上未必会有
  • 在github里搜索 “windows service”,发现有一个项目的星比较多
    https://github.com/kohsuke/winsw
    winsw 可以将Windows上的任何一个程序注册为服务
  • 下载winsw,并装备好配置文件,然后将winsw,zvingtools.exe及所有依赖,一起打包为自解压程序,解压到program files目录,并执行
winsw uninstall
winsw install
winsw start
  • 试执行这个自解压程序,在没有使用管理员身份运行的情况下,服务也启动起来了。
    使用PostMan尝试连接 http://localhost:10808/ 成功!
  • 耶,到此目标初步达成——用c#实现一个http服务接受ajax请求,c#实现http客户端上传文件到远程服务器
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant