
如何把服务器非项目目录下的图片在客户端显示?
1个回答
展开全部
#region 上传图片方法
/// <summary>
/// 上传图片方法
/// </summary>
/// <param name="linkImg">FileUpload控件</param>
/// <param name="links">预览图片img的名字</param>
/// <param name="file">上传图片的路径</param>
/// <returns></returns>
public string AddImg(FileUpload linkImg, HtmlImage links, string file)
{
string img = "";
if (linkImg.HasFile)//检查是否有文件
{
string fullFileName = linkImg.PostedFile.FileName; //文件路径名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1); //图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式
if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp" || type == "BNG" || type == "png") //判断是否为图片类型
{
string path = HttpContext.Current.Request.MapPath("~/" + file + "/");//获取上传文件的网站目录路径
linkImg.SaveAs(path + fileName);//存储文件到磁盘
if (links != null)
{
links.Src ="~/" + file + "/" + fileName;//显示图片
}
img ="~/"+ file + "/" + fileName;
}
else
{
HttpContext.Current.Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
}
}
else
{
HttpContext.Current.Response.Write("<script>alert('必须指定文件!');</script>");
}
return img;
}
#endregion
/// <summary>
/// 添加Flash按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
Flashs flash = new Flashs();
flash.FName = LinkURL.Text.Trim().ToString();
string url = this.AddImg(ImgURL, pic, "upload");
flash.FUrl = url;
int result = FlashManager.AddFlash(flash);
if (result > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!');</script>");
}
}
这些都是从项目里面拷贝出来的,可以直接使用的。有什么问题,再找糊涂。
/// <summary>
/// 上传图片方法
/// </summary>
/// <param name="linkImg">FileUpload控件</param>
/// <param name="links">预览图片img的名字</param>
/// <param name="file">上传图片的路径</param>
/// <returns></returns>
public string AddImg(FileUpload linkImg, HtmlImage links, string file)
{
string img = "";
if (linkImg.HasFile)//检查是否有文件
{
string fullFileName = linkImg.PostedFile.FileName; //文件路径名
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1); //图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式
if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp" || type == "BNG" || type == "png") //判断是否为图片类型
{
string path = HttpContext.Current.Request.MapPath("~/" + file + "/");//获取上传文件的网站目录路径
linkImg.SaveAs(path + fileName);//存储文件到磁盘
if (links != null)
{
links.Src ="~/" + file + "/" + fileName;//显示图片
}
img ="~/"+ file + "/" + fileName;
}
else
{
HttpContext.Current.Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
}
}
else
{
HttpContext.Current.Response.Write("<script>alert('必须指定文件!');</script>");
}
return img;
}
#endregion
/// <summary>
/// 添加Flash按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
Flashs flash = new Flashs();
flash.FName = LinkURL.Text.Trim().ToString();
string url = this.AddImg(ImgURL, pic, "upload");
flash.FUrl = url;
int result = FlashManager.AddFlash(flash);
if (result > 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加成功!');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!');</script>");
}
}
这些都是从项目里面拷贝出来的,可以直接使用的。有什么问题,再找糊涂。
追问
这个是图片上传呀,
我需要的是图片的显示,
取得的图片的流,并送给 客户端 显示出来
或者用其他方法,
谢谢
参考资料: 百度一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询