使用FileUpload 上传到文件夹中,上传成功后 图片是隐藏的,需要右键 包括在项目中才能用
下面是上传代码
uploadname = Server.MapPath("~/LogoImage/" + logoimage);
FileUploadLOGO.PostedFile.SaveAs(uploadname);如果不包括在项目中图片没办法在程序里面引用这是为什么,有什么办法解决??
下面是上传代码
uploadname = Server.MapPath("~/LogoImage/" + logoimage);
FileUploadLOGO.PostedFile.SaveAs(uploadname);如果不包括在项目中图片没办法在程序里面引用这是为什么,有什么办法解决??
if (FileUpload1.HasFile)
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);//文件的上传路径
imageAddress =" CPimage/"+ FileUpload1.FileName;
Response.Write("<script>alert('上传成功,请继续填写资料!')</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('文件不能上传.')</script>");
return;
}
}
else
{
Response.Write("<script>alert('请选择要上传的文件.')</script>");
return;
}解决了么
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;public partial class PhotoImage : BasePage
{
//获取连接字符串
string ConnStr = ConfigurationManager.ConnectionStrings["EXD"].ConnectionString; protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id;
if (Request.QueryString["Id"] != null)
{
string s = Request.QueryString["Id"];
id = Convert.ToInt32(s);
}
else
{
Response.Clear();
Response.ClearHeaders();
return;
} try
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand sqlcmd;
SqlParameter parameter;
conn.Open();
sqlcmd = new SqlCommand("PHOTO_IMAGE_SEL", conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
parameter = sqlcmd.Parameters.Add("@ID", SqlDbType.Int);
parameter.Direction = ParameterDirection.Input;
parameter.Value = id;
Object oImg = sqlcmd.ExecuteScalar();
if (oImg != DBNull.Value)
{ Response.Clear();
Response.ClearHeaders();
Response.ContentType = "image/jpeg";
Response.BinaryWrite((byte[])oImg); }
else
{
string path = Server.MapPath("Images/empty.jpg");
using (FileStream fs = File.OpenRead(path))
{
byte[] img = new byte[fs.Length];
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "image/jpeg";
fs.Read(img, 0, (int)(fs.Length));
Response.BinaryWrite(img);
}
}
}
}
catch (Exception)
{
Response.Clear();
Response.ClearHeaders();
}
}
}
}
FILEUPLOAD这个可是个坑死新手的控件
private string FileUpload()
{
string Path = "";
//用hasFile()判断是否有文件
try
{
//定义上传到服务器的路径
string path = Server.MapPath("~/File/");//创建在当前网站下
//路径不存在,创建
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
// string time = DateTime.Now.ToShortDateString() + DateTime.Now.Hour + DateTime.Now.Minute;
string fullname = FileUpload1.FileName;
Name = fullname;
//路径
string filename = path + fullname;
//?用SaveAs()来保存,在上传之前还可以做一些检查操作(大小和类型的过滤)
//判断大小
HttpPostedFile postFile = FileUpload1.PostedFile;
Length = postFile.ContentLength / 1024 + "k";
//应该判断文件大小,大于2MB的文件不能上传
FileUpload1.SaveAs(filename);
//作下载的操作
Path = "~/File/" + FileUpload1.FileName;
} catch
{
Lbltip.Text = "出现异常,请稍后再试!";
} return Path;
}