ASP.net上传图片只是一个空壳
同一段代码,上传.txt文件是完好的,上传图片和.exe文件就只有一个空壳,没有内容的
这是我的代码:
string filepath = FileUpload1.PostedFile.FileName.ToString();
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string serverpath = HttpContext.Current.Server.MapPath("file/") + filename;
int pos = filename.LastIndexOf(".");
string lastname = filename.Substring(pos, filename.Length - pos);
FileUpload1.PostedFile.SaveAs(serverpath);
protected void pictureUpLoad_Click(object sender, EventArgs e) { //获取图片名称 string fileName = picUpload.FileName; if (fileName != "") { //将图片名称都同意格式化yyyyMMddHHmmss+后缀 String randomStr = "Img" + DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); string lastName = randomStr + System.IO.Path.GetExtension(this.picUpload.FileName); //名字+后缀 //设置图片路径 string path = Server.MapPath("~/images/Joins/"); //保存图片 picUpload.SaveAs(path + lastName); txtImage.Text = lastName; } else { ui.Alert(this, "不能上传空文件"); } }
估计是您的路径写错了。
//获取全中的文件路径
string filepath = this.FileUpload1.FileName;
//获取文件名
string name = filepath.Substring(filepath.LastIndexOf('/')+1, filepath.Length);
//获取服务器路径
string path = Request.MapPath("file");
//保存
this.FileUpload1.SaveAs(path+"/"+name);
我没有设定上传文件的大小
/// <summary>
/// 点击上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
////获取全中的文件路径
//string filepath = this.FileUpload1.FileName;
////获取文件名
//string name = filepath.Substring(filepath.LastIndexOf('/')+1, filepath.Length);
////获取服务器路径
//string path = Request.MapPath("file");
////保存
//this.FileUpload1.SaveAs(path+"/"+name);
string filepath = FileUpload1.PostedFile.FileName.ToString();
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string serverpath = HttpContext.Current.Server.MapPath("file/") + filename;
int pos = filename.LastIndexOf(".");
string lastname = filename.Substring(pos, filename.Length - pos);
FileUpload1.PostedFile.SaveAs(serverpath);
}
上面注释了的就是我的,下面的是我复制的你的