要把本地的图片上传到服务器上该怎么写? 本地path E:\VSS 文件名 a.jpg服务器ip:192.168.4.111服务器path D:\My Documents 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //上传照片到image文件夹 protected void Button1_Click(object sender, EventArgs e) { string filename = FileUpload1.FileName;//文件名(文件类型,如hello.gif) string fileType=filename.Substring(filename.LastIndexOf(".")+1);//文件类型 string filePath = Server.MapPath(filename);//文件的物理地址 string serverPath = "";//存储服务器物理地址 double size = FileUpload1.PostedFile.ContentLength / 1024; if (size > 60) { Label1.Text = "图片大小不超过60kb"; } else { if (fileType == "bmp" || fileType == "jpg" || fileType == "gif") { string newName = DateTime.Now.ToFileTime() + "." + fileType; serverPath = Server.MapPath("192.168.4.111/My Documents/") + newName; FileUpload1.SaveAs(serverPath);//保存文件 Label1.Text = "上传成功!"; Image1.Visible = true; Image1.ImageUrl = "image/" + newName; } else { Label1.Text = "请选择正确的图片类型"; } } }试试,我也没试过,这是我将以前的拿来改的,因为没有传到服务器上过 string serverPath = Server.MapPath("~");//绝对路径 string serverfacePath = "/Accessories/" + CompanyId + "/" + StringHelper.GetDate() + "/"; DirectoryInfo companyDirectory = new DirectoryInfo(serverPath + serverfacePath); if (!companyDirectory.Exists) { companyDirectory.Create(); } if (fileAddPic.HasFile) { //FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG }; string fileContentType = fileAddPic.PostedFile.ContentType; if (fileContentType.StartsWith("image")) { string oldImg = imgPhoto.ImageUrl; if (oldImg.Length > 2) { string delImg = serverPath + oldImg.Substring(2); if (File.Exists(delImg)) { File.Delete(delImg); } } string fileExtension = System.IO.Path.GetExtension(fileAddPic.FileName);//扩展名 string fileTrueName = StringHelper.GetTime() + StringHelper.GetRandomNumberString(5) + fileExtension; fileAddPic.SaveAs(serverPath + serverfacePath + fileTrueName); bsProducts.UpdateProductPic(CompanyId, proId, serverfacePath + fileTrueName); } else { ScriptManager.RegisterStartupScript(this.upAddPic, this.GetType(), "error", "alert('只能上传图片文件!');", true); return; } } c#ComboBox控件动态绑定数据 一个简单的asp.net 问题。 请求大家给我推荐个小软件 引用多个母版页 如何定义个做除法的函数,并将结果返回呢? 求助 关于C++ 的 IDCARD_ALL & card 参数如何在C#中调用 在C#中如何实现数字与字符串的转换?? 关于链接池的问题? VS调试相关 求指点!VS里面的一个效果是如何实现的 winfrom 保存从数据库读取的数据? 这是不是C#的一个语言漏洞啊
//上传照片到image文件夹
protected void Button1_Click(object sender, EventArgs e)
{
string filename = FileUpload1.FileName;//文件名(文件类型,如hello.gif)
string fileType=filename.Substring(filename.LastIndexOf(".")+1);//文件类型
string filePath = Server.MapPath(filename);//文件的物理地址
string serverPath = "";//存储服务器物理地址 double size = FileUpload1.PostedFile.ContentLength / 1024; if (size > 60)
{
Label1.Text = "图片大小不超过60kb";
}
else
{
if (fileType == "bmp" || fileType == "jpg" || fileType == "gif")
{
string newName = DateTime.Now.ToFileTime() + "." + fileType;
serverPath = Server.MapPath("192.168.4.111/My Documents/") + newName;
FileUpload1.SaveAs(serverPath);//保存文件
Label1.Text = "上传成功!";
Image1.Visible = true;
Image1.ImageUrl = "image/" + newName;
}
else
{
Label1.Text = "请选择正确的图片类型";
}
}
}试试,我也没试过,这是我将以前的拿来改的,因为没有传到服务器上过
string serverfacePath = "/Accessories/" + CompanyId + "/" + StringHelper.GetDate() + "/";
DirectoryInfo companyDirectory = new DirectoryInfo(serverPath + serverfacePath); if (!companyDirectory.Exists)
{
companyDirectory.Create();
} if (fileAddPic.HasFile)
{
//FileExtension[] fe = { FileExtension.GIF, FileExtension.JPG, FileExtension.PNG };
string fileContentType = fileAddPic.PostedFile.ContentType;
if (fileContentType.StartsWith("image"))
{
string oldImg = imgPhoto.ImageUrl;
if (oldImg.Length > 2)
{
string delImg = serverPath + oldImg.Substring(2);
if (File.Exists(delImg))
{
File.Delete(delImg);
}
} string fileExtension = System.IO.Path.GetExtension(fileAddPic.FileName);//扩展名
string fileTrueName = StringHelper.GetTime() + StringHelper.GetRandomNumberString(5) + fileExtension;
fileAddPic.SaveAs(serverPath + serverfacePath + fileTrueName);
bsProducts.UpdateProductPic(CompanyId, proId, serverfacePath + fileTrueName);
}
else
{
ScriptManager.RegisterStartupScript(this.upAddPic, this.GetType(), "error", "alert('只能上传图片文件!');", true);
return;
}
}