如何将基于乳胶图像的图像转换为可下载的网址

时间:2019-04-17 10:55:25

标签: asp.net

我有一个URL“ https://latex.codecogs.com/gif.latex?%5Csmall%20%5Cfrac%7Br%7D%7Bs%7D”,当在asp.net应用程序中将其作为WebClient的输入时,必须下载图像。

使用this website进行转换,从url获取图像。

test.aspx.cs

string url = "https://latex.codecogs.com/gif.latex?%5Csmall%20%5Cfrac%7Br%7D%7Bs%7D";
WebClient webClient = new WebClient();
string path = Server.MapPath("Images"); // Create a folder named 'Images' in your root directory
string fileName = Path.GetFileName(url);
webClient.DownloadFile(url, path + "\\" + fileName);

1 个答案:

答案 0 :(得分:0)

我认为您想这样做。下载图片,然后直接在您的网站上下载该图片。但是您只需一行代码即可将其保存到磁盘。

protected void Button11_Click(object sender, EventArgs e)
{
    string url = "https://latex.codecogs.com/gif.latex?%5Csmall%20%5Cfrac%7Br%7D%7Bs%7D";
    string fileName = "MyImage.png";

    //create a new webclient
    using (WebClient client = new WebClient())
    {
        //download the external file
        byte[] bin = client.DownloadData(url);

        //save it do disk
        File.WriteAllBytes(Server.MapPath("/") + fileName, bin.ToArray());

        //or let your user download it directly
        Response.ClearHeaders();
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "image/png";
        Response.AddHeader("content-length", bin.Length.ToString());
        Response.AddHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", fileName));
        Response.OutputStream.Write(bin, 0, bin.Length);
        Response.Flush();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
}