Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 无效的虚拟路径-尝试从url返回文件时_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 无效的虚拟路径-尝试从url返回文件时

Asp.net mvc 3 无效的虚拟路径-尝试从url返回文件时,asp.net-mvc-3,Asp.net Mvc 3,我们从CdN下载一个文件,然后将下载文件的url返回给用户。我正试图实现这一点,以便当用户单击下载按钮时,它会去测试下载文件的url,然后根据该本地url强制执行保存提示 因此,例如,如果页面上有一个名为“下载”的按钮用于特定的.pdf文件,那么我们的控制器中最终会有代码转到cdn并下载文件,压缩文件,然后返回url,例如: 我不确定是否可以使用File方法将资源返回给用户,因为当您有文件的url而不是系统目录虚拟路径时,会出现保存提示 那么,我怎样才能让它与url一起工作呢?我需要下载按钮,以

我们从CdN下载一个文件,然后将下载文件的url返回给用户。我正试图实现这一点,以便当用户单击下载按钮时,它会去测试下载文件的url,然后根据该本地url强制执行保存提示

因此,例如,如果页面上有一个名为“下载”的按钮用于特定的.pdf文件,那么我们的控制器中最终会有代码转到cdn并下载文件,压缩文件,然后返回url,例如:

我不确定是否可以使用File方法将资源返回给用户,因为当您有文件的url而不是系统目录虚拟路径时,会出现保存提示

那么,我怎样才能让它与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);
    }

错误:无效的虚拟路径

除非zip文件在您的虚拟路径中,否则此操作无效

这里使用的File方法Filestring,string,string需要一个文件名,该文件名将用于创建FilePathResult

另一个选项是使用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);

除非zip文件在您的虚拟路径中,否则这将不起作用

这里使用的File方法Filestring,string,string需要一个文件名,该文件名将用于创建FilePathResult

另一个选项是使用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);