.net core webapi 轉成數據流

最近在使用ueditor時,調取後端配置文件,以前是asp.net的官方已有案例javascript

如今轉爲 webapi時,須要將配置文件轉成response數據流java

通常咱們使用webapi時,都是將數據轉換成一個實體或者string來輸出,這些方式都沒法使用與ueditor調用後臺配置文件的方式web

ueditor從服務器返回的是一段javascript腳本,代碼以下json

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);
        }
相關文章
相關標籤/搜索