我有这个功能:
<code>
var express = require('express');
var router = express.Router();
// use session auth to secure the angular app files
router.use('/', function (req, res, next) {
if (req.path !== '/login' && !req.session.token) {
return res.redirect('/login?returnUrl=' + encodeURIComponent('/app' + req.path));
}
next();
});
// make JWT token available to angular app
router.get('/token', function (req, res) {
res.send(req.session.token);
});
// serve angular app files from the '/app' route
router.use('/', express.static('app'));
module.exports = router;
saveFileLocation 是我目录中的文件路径。我想通过document.location将它作为字符串传递,因为我想下载它,但是当我这样做时会返回一个错误的请求(我想这是因为反斜杠)。
saveFileLocation值为 function PrintCApplication() {
cApplication.PrintApplication(applicationID,
function (isSuccessful, saveFileLocation) {
if (isSuccessful) {
document.location = '/CApplication/DownloadFile/' + saveFileLocation;
}
});
};
这是我的示例网址无效。 http://localhost:4617/CApplication/DownloadFile/C:/Users/Me/Documents/CApplicationTemplate.xlsx
问题:有没有办法可以通过document.location作为字符串传递文件路径?如果是的如何?如果不是,还有其他方式吗?
答案 0 :(得分:1)
您只能使用javascript重定向到相对路径:
window.location.href = '/MyWebsite/DownloadFile/' + saveFileLocation;
请注意,这是您的网络应用程序目录的相对路径 。您不能使用javascript重定向到C://驱动器文件位置。解释原因在于:Go to local URL with Javascript
答案 1 :(得分:1)
假设您使用的是IIS(Visual Studio)
您只能访问根路径网站Server.MapPath("~")
下公开的文件,并在您的web.config文件中添加.xlsx标题。
Server.MapPath("~/mySubfolder/CApplicationTemplate.xlsx")
。在WebForm中:
<asp:Button OnClick="btnExcel_Click" />/
代码背后:
private void btnExcel_Click(object sender, System.EventArgs e)
{
var fileName = "C:/Users/Me/Documents/CApplicationTemplate.xlsx";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (var fs = File.OpenRead(fileName))
{
ms.SetLength(fs.Length);
fs.Read(ms.GetBuffer(), 0, Convert.ToInt32(fs.Length));
}
//Clear headers
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
//Add my send my xlsx file
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
Response.AddHeader("Content-Disposition", "attachment; filename=CApplicationTemplate.xlsx");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Expires", "0");
ms.WriteTo(Response.OutputStream);
Response.Flush();
ms.Dispose();
}
在web.config文件中添加以下配置:
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".xslx" mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
</staticContent>
</system.webServer>
</configuration>