#c# #asp.net #download
#c# #asp.net #Скачать
Вопрос:
Я пытаюсь загрузить файл с ретранслятора. Ниже приведен мой код:
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.MapPath("~/Data/") filePath);
Response.WriteFile(filePath);
Response.End();
}
Я получаю следующее исключение:
Файл сохраняется в папке данных папки маршрута:
Как мне написать код, чтобы найти фактический путь?
Ответ №1:
Похоже, ваш путь к файлу неверен (./College/Graphic1.jpg ), он не соответствует пути, указанному на скриншоте (./Data/Graphic1.jpg )
Комментарии:
1. ДА. Вот почему я задал вопрос, какой правильный код для получения фактического пути?
2. ? Измените CommandArgument на вашей кнопке LinkButton на правильный путь?
Ответ №2:
Я решил свой вопрос. Я изменил свой код на следующий код:
protected void DownloadFile(object sender, EventArgs e)
{
WebClient req = new WebClient();
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
HttpResponse response = HttpContext.Current.Response;
string file = Server.MapPath("~/Data/") filePath;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=" filePath);
byte[] data = req.DownloadData(file);
response.BinaryWrite(data);
response.End();
}