上传图片出错,用的是月儿清清的那段代码!路径问题么,请问怎么改!谢谢
string filepath = FileUpload1.PostedFile.FileName;
if (IsAllowedExtension(FileUpload1) == true)
{
string filename = filepath.Substring(filepath.LastIndexOf( "\\ ") + 1);
string serverpath = Server.MapPath( "images/ ") + System.DateTime.Now.ToString( "yyy-MM-dd-hh-mm-ss ") + Session.SessionID + filename;
//string serverpath2 = Server.MapPath( "images/ ") + System.DateTime.Now.ToString( "yyy-MM-dd-hh-mm-ss ") + Session.SessionID + filename;
FileUpload1.PostedFile.SaveAs(serverpath);
上传发生错误!原因:
System.IO.FileNotFoundException: 未能找到文件“C:\Documents and Settings\Administrator\My Documents\1.jpg”。
------解决方案--------------------你是用input(file)浏览要上传的文件的吗?
下面是我上传xls文件的代码,你修改下就可以用啦,我的是先用input(file)浏览要上传的文件,再单击BUTTON,就可以完成上传的了
protected void Button3_Click(object sender, EventArgs e)
{
string Path;
string FileName;
int Po;
long FileSize;
string fType;
string xmlfile = " ";
// if(this.File1.PostedFile.FileName!= " ")
if (File1.PostedFile != null)
{ xmlfile = File1.PostedFile.FileName; }
if (File1.PostedFile.FileName != " ") //PostFile 's Filename is not empty
{
try
{
Path = Server.MapPath( "\\NShop\\UpFile ").ToString();
FileName = File1.PostedFile.FileName.ToString();
//fType = File1.PostedFile.ContentType;
FileSize = File1.PostedFile.ContentLength;
if (FileName.Substring(FileName.Length - 4, 4) != ".xls ") // "application/vnd.ms-excel " fType string
{
Label1.Text = "所选择的不是Excel文件,请选择Excel文件! ";
return;
}
Po = FileName.LastIndexOf((char)92);
FileName = FileName.Substring(Po + 1, FileName.Length - Po - 1);
ViewState[ "PostFileName "] = FileName; //Remember PostFile 's name
File1.PostedFile.SaveAs(Path + "\\ " + FileName);
Label1.Text = "Excel文件上传成功! ";
}
catch (Exception er)
{