图片上传代码如下:
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/jpg")
{ string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
string file = name.Substring(name.LastIndexOf("\\") + 1);
string fileName = file; // 文件名称
string fileName_s = "s_" + file; // 缩略图文件名称
string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath("file/" + fileName_s); // 服务器端缩略图路径
if (!File.Exists(webFilePath))
{ // 使用 SaveAs 方法保存文件
FileUpload1.PostedFile.SaveAs(webFilePath_s);
FileUpload1.SaveAs(webFilePath);
this.Image1.ImageUrl = "file/" + file; }
}
我把图片传到了我的工程下的file文件夹里,在ACCESS数据库中存放图片路径,但是存放的路径是
F:\MyWork\新闻管理系统\NewsNet\file\s_2.jpg,我想要得到的路径是file\s_2.jpg
怎么办啊?帮帮忙,因为,只要一离开我这台机子,到别的机子上就无法显示出来图片,急啊!
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/jpg")
{ string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
string file = name.Substring(name.LastIndexOf("\\") + 1);
string fileName = file; // 文件名称
string fileName_s = "s_" + file; // 缩略图文件名称
string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
string webFilePath_s = Server.MapPath("file/" + fileName_s); // 服务器端缩略图路径
if (!File.Exists(webFilePath))
{ // 使用 SaveAs 方法保存文件
FileUpload1.PostedFile.SaveAs(webFilePath_s);
FileUpload1.SaveAs(webFilePath);
this.Image1.ImageUrl = "file/" + file; }
}
我把图片传到了我的工程下的file文件夹里,在ACCESS数据库中存放图片路径,但是存放的路径是
F:\MyWork\新闻管理系统\NewsNet\file\s_2.jpg,我想要得到的路径是file\s_2.jpg
怎么办啊?帮帮忙,因为,只要一离开我这台机子,到别的机子上就无法显示出来图片,急啊!
lz没有把存放数据库部分的代码贴出来,而贴了段无关的代码.
LZ应该把"file/" + file保存到数据库里
/// <summary>
/// 上传文件 使用方法 Core.File.upload(File1, new string[] { "gif", "jpg", "png", "bmp" }, "imgs/uploadimages/",150);
/// </summary>
/// <param name="File1">上传的控件</param>
/// <param name="str">要隔离的后缀名</param>
/// <param name="path">保存的路径 (最后要加 "/")</param>
/// <param name="number">图片的大小</param>
/// <returns>保存到数据库的路径 后面加\\ 前面不用加</returns>
public static string upload(System.Web.UI.HtmlControls.HtmlInputFile File1, string[] str, string path,int number)
{
string serverPath = System.Web.HttpContext.Current.Server.MapPath(".") + "//";//服务器路径
string savePath = "";//用于返回的路径 保存到数据库的
string upPath = "";//上传的路径;
string name = DateTime.Now.ToString("yyyyMMddHHmmss");//重新命名
bool bl = false;//用于判断文件后缀
if (File1.Value != "")
{
string str1 = File1.Value;//用户给的路径
string houzhui = str1.Substring(str1.LastIndexOf(".") + 1).ToLowerInvariant();//后缀名
if (houzhui.Length < 3 && houzhui.Length >= 0)
{
houzhui.PadRight(3);
}
//判断图片格式是否正确
for (int i = 0; i < str.Length; i++)
{
if (houzhui == str[i].ToString())
{
bl = true;
break;
}
}
if (bl)
{
savePath = path + name + "." + houzhui;
upPath = System.Web.HttpContext.Current.Server.MapPath("~/") + path + name + "." + houzhui;
File1.PostedFile.SaveAs(upPath);//上传截图
}
}
//生成缩略图 //string houzhui = File1.Value.Substring(File1.Value.LastIndexOf('.'));//得到后缀
//System.Threading.Thread.Sleep(1000);//挂起10毫秒时间
//string savePath = DateTime.Now.ToString("yyyyMMddHHmmss") + houzhui;//得到新生成的图片 System.Drawing.Bitmap image2 = new System.Drawing.Bitmap(System.Web.HttpContext.Current.Server.MapPath("~/") + savePath);//原图 int width = image2.Width;//得到原图的宽
int height = image2.Height;//得到原图的高 //判断图片宽高
if (width > height)//如果宽比高大 则按照宽等比例缩小或放大
{
if (width >= number)//如果宽要比规定的大 则缩小
{
width = number;//得到宽度
height = number * image2.Height / image2.Width;//得到等比例缩小的高度
}
}
else
{
if (height >= number)//如果高要比规定的大 则缩小
{
height = number;//得到高度
width = number * image2.Width / image2.Height;//得到等比例缩小的宽度
}
} System.Drawing.Image image = image2.GetThumbnailImage(width, height, null, new IntPtr());//给图片创建缩略图
image2.Dispose();
Core.File.Delete(System.Web.HttpContext.Current.Server.MapPath("~/") + savePath);//删除原有图片 image.Save(System.Web.HttpContext.Current.Server.MapPath("~/") + savePath, System.Drawing.Imaging.ImageFormat.Bmp);//保存绘画好的图片 image.Dispose();
return savePath;
}
F:\MyWork\新闻管理系统\NewsNet\file\s_2.jpg,我想要得到的路径是file\s_2.jpg ================数据库中存储相对路径,比如你可以存储 file\s_2.jpg如何显示:
myImage.Url = Reqeust.ApplicationPath + (Reqeust.ApplicationPath == "/" ? "" : "/") + <数据库图片路径>; // 假设 file 文件在网站跟目录