在ASP.NET中,可以使用FileUpload控件来实现文件上传功能。但是,FileUpload控件只能上传客户端计算机上的文件,无法直接上传外部图片URL。要实现上传外部图片URL的功能,可以通过以下步骤来实现:
protected void btnUpload_Click(object sender, EventArgs e)
{
string imageUrl = txtImageUrl.Text;
string serverPath = Server.MapPath("~/Images/"); // 保存图片的服务器路径
using (WebClient client = new WebClient())
{
try
{
// 从外部URL下载图片
string fileName = Path.GetFileName(imageUrl);
string filePath = Path.Combine(serverPath, fileName);
client.DownloadFile(imageUrl, filePath);
// 文件上传成功的处理逻辑
// ...
// 清空输入框
txtImageUrl.Text = "";
}
catch (Exception ex)
{
// 文件上传失败的处理逻辑
// ...
}
}
}
在上述代码中,使用WebClient类的DownloadFile方法从外部URL下载图片,并保存到服务器上指定的路径。你可以根据实际需求修改保存路径和处理逻辑。