总的问题是怎么用javascript+C#来将网页上的图片保存到本地。
我现在已经获得了图片的url,也会用C#将指定url的图片保存到本地,但我的要求是在下面这段代码里实现,每3秒就将网页中的第一个图片元素保存到本地。 addr就是图片的url怎么样实现保存到本地。
//每3秒平移一段,点鼠标左键结束
var t=setInterval(moves,3000);
var i = 0;
function moves() {
i++;
var x = document.getElementsByTagName(“img”);
var addr = x[0].src;
//alert(addr);
map.panTo(new BMap.Point(116.31557 + i * 0.029078, 39.93381));
}
map.addEventListener(“click”, function() {
clearInterval(t);
});
//每3秒平移一段,点鼠标左键结束
我在网上找到一个C#保存图片到本地的代码?使用一个类实现的。关键是怎么将前后联系起来
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
/*
* Name: 通过URL保存图片到本地
*
* Author:Sungan
*
* Date:2011.7.19
* *
*/
namespace SaleMapGoogle
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]//将该类设置为com可访问
public class LoadImag
{
///
/// 从Url保存图片到本地
///
///
///
///
public bool SavePhotoFromUrl(string FileName, string Url)
{
bool value = false;
WebResponse response = null;
Stream stream = null;
try
{
WebRequest request = WebRequest.Create(Url);
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith(“text/”))
{
value = SaveBinaryFile(response, FileName);
}
}
catch (Exception err)
{
string aa = err.ToString();
}
return value;
}
///
///
///
///
///
///
private bool SaveBinaryFile(WebResponse response, string FileName)
{
bool Value = true;
byte[] buffer = new byte[1024];
try
{
if (!File.Exists(FileName))
{
Stream outStream = System.IO.File.Create(FileName);
Stream inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
}
while (l > 0);
outStream.Close();
inStream.Close();
}
}
catch
{
Value = false;
}
return Value;
}
}
}