嗨,我想在 MVC 中创建文件并将 JSON 数据插入到该文件中。 创建该文件的文件夹位置是
~/内容/文件
请提出任何建议
最佳答案
public FileResult SaveJsonDataToFile(string JsonString)
{
#region " Set Path of File "
// ASP.NET MVC4
string FilePath = HttpContext.Server.MapPath("~/Content/File/jsondata.json");
// ASP.NET MVC1 -> MVC3
// use this HttpContext.Current.Server.MapPath("~/Content/File/jsondata.json");
// Ref : http://stackoverflow.com/questions/1268738/asp-net-mvc-find-absolute-path-to-the-app-data-folder-from-controller
#endregion
#region " Save Data to File "
using (System.IO.StreamWriter FWriter = new StreamWriter(FilePath, true))
{
FWriter.Write(JsonString);
}
#endregion
#region " Read File to FileData "
byte[] FileData = null;
FileData = System.IO.File.ReadAllBytes(FilePath);
#endregion
#region " Delete File "
if (System.IO.File.Exists(FilePath))
System.IO.File.Delete(FilePath);
#endregion
// return if you need to send file to client
return File(FileData, "text/json" /* i don't sure for json file mimetype */);
}
此示例代码是 C# .Net MVC
希望这有帮助!
关于angularjs - 如何在MVC中创建文件并向其中插入json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083174/