.net core webapi 转成数据流

最近在使用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);
        }

 

转载于:https://my.oschina.net/kee1986/blog/3008805

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值