最近在使用ueditor时,调取后端配置文件,之前是asp.net的官方已有案例
现在转为 webapi时,需要将配置文件转成response数据流
一般我们使用webapi时,都是将数据转换成一个实体或者string来输出,这些方式都无法使用与ueditor调用后台配置文件的方式
ueditor从服务器返回的是一段javascript脚本,代码如下
bd__editor__89sg27({"imageActionName":"uploadimage","imageFieldName":"upfile","imageMaxSize":2048000,"imageAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"imageCompressEnable":true,"imageCompressBorder":1600,"imageInsertAlign":"none","imageUrlPrefix":"http://image.keesoft.cn/api/ueditor/","imagePathFormat":"upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","scrawlActionName":"uploadscrawl","scrawlFieldName":"upfile","scrawlPathFormat":"upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","scrawlMaxSize":2048000,"scrawlUrlPrefix":"/ueditor/net/","scrawlInsertAlign":"none","snapscreenActionName":"uploadimage","snapscreenPathFormat":"upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","snapscreenUrlPrefix":"/ueditor/net/","snapscreenInsertAlign":"none","catcherLocalDomain":["127.0.0.1","localhost","img.baidu.com"],"catcherActionName":"catchimage","catcherFieldName":"source","catcherPathFormat":"upload/image/{yyyy}{mm}{dd}/{time}{rand:6}","catcherUrlPrefix":"/ueditor/net/","catcherMaxSize":2048000,"catcherAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"videoActionName":"uploadvideo","videoFieldName":"upfile","videoPathFormat":"upload/video/{yyyy}{mm}{dd}/{time}{rand:6}","videoUrlPrefix":"/ueditor/net/","videoMaxSize":102400000,"videoAllowFiles":[".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid"],"fileActionName":"uploadfile","fileFieldName":"upfile","filePathFormat":"upload/file/{yyyy}{mm}{dd}/{time}{rand:6}","fileUrlPrefix":"/ueditor/net/","fileMaxSize":51200000,"fileAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"],"imageManagerActionName":"listimage","imageManagerListPath":"upload/image","imageManagerListSize":20,"imageManagerUrlPrefix":"/ueditor/net/","imageManagerInsertAlign":"none","imageManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"fileManagerActionName":"listfile","fileManagerListPath":"upload/file","fileManagerUrlPrefix":"/ueditor/net/","fileManagerListSize":20,"fileManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"]})
所以webapi必须以同样的方式返回javascript脚本,所有webapi只能通过输出文本流来解决这个问题
[HttpGet][Route("Test")]public async Task Test(string actionType, string callback) { Response.ContentType = "application/javascript"; string strPath = Path.Combine(Directory.GetCurrentDirectory()) + @"\config.json"; StreamReader sr = new StreamReader(strPath); string strText = sr.ReadToEnd(); sr.Close(); object json = Newtonsoft.Json.JsonConvert.DeserializeObject(strText); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(callback + "(" + Newtonsoft.Json.JsonConvert.SerializeObject(json) + ")"); await Response.Body.WriteAsync(bytes, 0, bytes.Length); }