2016-09-16 29 views
1

我有这样的代码:剃刀+ JS未捕获的SyntaxError:无效的或意外的标记

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

,当我检查我的网页我得到这个错误:

Uncaught SyntaxError: Invalid or unexpected token

而此行有下划线:

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

这可能是一个愚蠢的解决方案,但我似乎无法找到它。我希望我的Blob网址为C:\ WINDOWS \ TEMP \ image.png这样我就可以再使用它像这样:

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

编辑: 我不希望因为我有铁杆这个临时路径不知道它是否永远是一样的。

回答

1

下面的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);

为了实现这个使用剃须刀,你可以做到以下几点:

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

解决它,谢谢!忘了那个:/ – DCalic

相关问题