我有以下代码:

var tempPath = "@Path.GetTempPath()";
var blobURL = tempPath + "image.jpg";


检查页面时出现此错误:


  未捕获的SyntaxError:无效或意外的令牌


此行带有下划线:

var tempPath = "C:\Windows\TEMP\";


这可能是一个愚蠢的解决方案,但我似乎找不到。我希望我的Blob-URL为C:\ Windows \ TEMP \ image.png,这样我就可以像这样使用它:

<img src="C:\Windows\TEMP\image.png"/>


编辑:
我不想改变这个临时路径,因为我不知道它是否总是一样。

最佳答案

以下JavaScript无效:



var tempPath = "C:\Windows\TEMP\";  // look at code highlighting
console.log(tempPath); // and at the result of execution





问题在于\反斜杠应在JS字符串中转义。
您需要通过以下方式转义反斜杠:



var tempPath = "C:\\Windows\\TEMP\\";
console.log(tempPath);





为了使用Razor实现此目的,您可以执行以下操作:

var tempPath = "@HttpUtility.JavaScriptStringEncode(Path.GetTempPath())";

08-06 12:17