如果我有下载的网址,www.website.com/myfile.html
因此,当点击该链接时,它会自动开始下载,例如,myfile.txt
,如何将该文件导入C#进行阅读..
这是Net.WebRequest.Create(url)
,Net.HttpWebRequest
的作用吗?
答案 0 :(得分:1)
您可以使用WebClient实现此目的:
using (var client = new WebClient())
{
// Download and save the file locally
client.DownloadFile("http://www.website.com/myfile.html", "myfile.html");
}
如果您不想在本地存储文件但只读取内容,可以尝试:
using (var client = new WebClient())
{
string result = client.DownloadString("http://www.website.com/myfile.html");
}
答案 1 :(得分:0)
以C#为例,以下是点击按钮,链接等后强制下载文件的方法......
public void DownloadFileLink_Click(object sender, EventArgs e)
{
//Get the file data
byte[] fileBytes = Artifacts.Provider.GetArtifact(ArtifactInfo.Id);
//Configure the response header and submit the file data to the response stream.
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment;filename=" + "myDynamicFileName.txt");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(fileBytes);
HttpContext.Current.Response.End();
}
考虑到这一点,您需要查找的是响应中的标题,标题将包含项目内容处置,其中包含在响应中流式传输的文件的文件名