关于网友提出的“问题背后的问题读后感 图片上传问题”问题疑问,本网通过在网上对“问题背后的问题读后感 图片上传问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:问题背后的问题读后感 图片上传问题描述:
string Path = FileUpload.PostedFile.FileName;
string PictureName1 = FileUpload.FileName;
Load = Server.MapPath("~/UpLoad/") + PictureName1;
FileUpload.PostedFile.SaveAs(Load);
想做一个图片上传功能
但貌似运行时有问题 不知道哪错了 求解
想知道 这里的代码有啥问题
解决方案1:
FileUpload.PostedFile.SaveAs(Server.MapPath("~/UpLoad/") + "\\" + PictureName1);
这里首先要你的UpLoad文件夹是存在的才行。你再仔细看下
using System;解决方案3:
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;//上传文件名字
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type = FileUpload1.PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string ipath = Server.MapPath("upimg") + "\\" + name;
string fpath = Server.MapPath("upfile") + "\\" + name;
string path="F:\\aaa\\"+FileUpload1.FileName;
string wpath = "upimg\\" + name;
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{
FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
// Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
Label1.Text = "你传图片的名字是" + name + "
文件大小为" + size + "
文件类型为" + type2 + "
文件路径为" + ipath;
}
SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{ SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
cn.Open();
string a = cmd.ExecuteScalar().ToString();
cn.Close();
Image1.ImageUrl = "F:\\aaa\\" + a;
}
}
//其他一些后缀名或为空的自己判断下
string path = Server.MapPath("images/");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string filename = path + Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(filename);