2012-04-02 62 views
2

我们从我们的CdN下载一个文件,然后将该文件的url返回给用户。我试图让这个实现,以便当用户点击下载buttton,它会去测试该网址到下载的文件,然后强制保存提示基于该本地URL。不是一个有效的虚拟路径 - 当试图从url中返回一个文件时

因此,举例来说,如果有一个名为下载页面上特定的.pdf按钮,我们最终有代码在我们的控制器去到CDN和下载文件,压缩和解则返回一个URL,例如:http://www.ourLocalAssetServer.com/assets/20120331002728.zip

我不确定您是否可以使用File()方法将资源返回给用户,以便在您拥有该文件的url而不是系统目录虚拟路径时导致保存提示。

所以我怎么能得到这个与网址工作?我需要下载按钮来最终强制一个保存提示,在他们的最后给出一个url,比如上面这个例子生成的内容?不是我使用的是POST,也不是GET,所以不知道在这种情况下我应该使用哪一种,除此之外不能全面强制保存提示。这是击中我的GetFileDownloadUrl,但最终错误说它不是一个虚拟路径。

这里是我的代码:

@foreach (CarFileContent fileContent in ModelCarFiles) 
{ 
    using (Html.BeginForm("GetFileDownloadUrl", "Car", FormMethod.Get, new { carId = Model.CarId, userId = Model.UserId, @fileCdnUrl = fileContent.CdnUrl })) 
    { 
     @Html.Hidden("userId", Model.UserId); 
     @Html.Hidden("carId", Model.CarId); 
     @Html.Hidden("fileCdnUrl", fileContent.CdnUrl);   
     <p><input type="submit" name="SubmitCommand" value="download" /> @fileContent.Name</p> 
    } 
} 

    public ActionResult GetFileDownloadUrl(string fileCdnUrl, int carId, int userId) 
    { 
     string downloadUrl = string.Empty; 

     // take the passed Cdn Url and go and download that file to one of our other servers so the user can download that .zip file 
     downloadUrl = GetFileZipDownloadUrl(carId, userId, fileCdnUrl; 

     // now we have that url to the downloaded zip file e.g. http://www.ourLocalAssetServer.com/assets/20120331002728.zip 
     int i = downloadUrl.LastIndexOf("/"); 

     string fileName = downloadUrl.Substring(i); 

     return File(downloadUrl, "application/zip", fileName); 
    } 

错误:不是有效的虚拟路径

回答

0

这会不会只是压缩文件的工作在你的虚拟路径。

您在此使用的File方法File(string,string,string)需要一个将用于创建FilePathResult的fileName。

另一种选择是下载它(使用WebClient.DownloadData或DownloadFile方法)并传递字节数组或文件路径(取决于您选择哪个)。

var webClient = new Webclient(); 
byte[] fileData = webClient.DownloadData(downloadUrl); 

return File(fileData, "application/zip", fileName); 

你在哪里得到的“/”刚拿到的文件名是不必要的,因为你也可以使用索引中的台词:

string fileName = System.IO.Path.GetFileName(downloadUrl);