jQuery或者js保存文件到本地

jQuery或者js保存文件到本地,第1张

function fake_click(obj) {

    var ev = document.createEvent("MouseEvents")

    ev.initMouseEvent(

        "click", true, false, window, 0, 0, 0, 0, 0

        , false, false, false, false, 0, null

        )

    obj.dispatchEvent(ev)

}function export_raw(name, data) {

    var urlObject = window.URL || window.webkitURL || window

    var export_blob = new Blob([data])

    var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")

    save_link.href = urlObject.createObjectURL(export_blob)

    save_link.download = name

    fake_click(save_link)

}

这个用jquery是无法实现的,需要用asp.net才能实现

using System.Net

using System.IO

using System.Drawing.Imaging

/// <summary>

/// 下载远程图片保存到本地

/// </summary>

/// <param name="savedir">本地保存路径</param>

/// <param name="imgpath">远程图片文件</param>

/// <returns></returns>

public string downRemoteImg(string savedir,string imgpath)

{

if (string.IsNullOrEmpty(imgpath))

return string.Empty

else

{

string imgName = string.Empty

string imgExt = string.Empty

string saveFilePath = string.Empty

imgName = imgpath.Substring(imgpath.LastIndexOf("/"), imgpath.Length - imgpath.LastIndexOf("/"))

imgExt = imgpath.Substring(imgpath.LastIndexOf("."), imgpath.Length - imgpath.LastIndexOf("."))

saveFilePath = Server.MapPath(savedir)

if (!Directory.Exists(saveFilePath))

Directory.CreateDirectory(saveFilePath)

try

{

WebRequest wreq = WebRequest.Create(imgpath)

wreq.Timeout = 10000

HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse()

Stream s = wresp.GetResponseStream()

System.Drawing.Image img

img = System.Drawing.Image.FromStream(s)

switch (imgExt.ToLower())

{

case ".gif":

img.Save(saveFilePath + imgName, ImageFormat.Gif)

break

case ".jpg":

case ".jpeg":

img.Save(saveFilePath + imgName, ImageFormat.Jpeg)

break

case ".png":

img.Save(saveFilePath + imgName, ImageFormat.Png)

break

case ".icon":

img.Save(saveFilePath + imgName, ImageFormat.Icon)

break

case ".bmp":

img.Save(saveFilePath + imgName, ImageFormat.Bmp)

break

}

img.Dispose()

s.Dispose()

return savedir + imgName

}

catch

{

return imgpath

}

}

}

使用方法

如保存到本地的test目录:

Response.Write(this.downRemoteImg("test", "http://www.xxx.com/123.gif"))


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11852169.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存