2016-09-16 60 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

相關問題