上传图片出错,用的是月儿清清的那段代码!路径问题么,请问怎么改!谢谢
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)
{
Label1.Text = er.Message;
}
}
else
{ Label1.Text = "请先选择要上传的文件! "; }
}
[解决办法]
up
(.net软件外包交流群邀请您的加入 :45638537,我建这个群的初衷是 为有这方面需求的朋友提供一个交流的平台,以方便大家寻找同一城市的合作伙伴)
[解决办法]
string imageFileName = Request.Files[ "resourceIcon "].FileName.ToString();
imageFileName = Guid.NewGuid().ToString() + imageFileName.Substring(imageFileName.LastIndexOf( ". "));
string savePath = Utility.GetUploadFilePath(Channels.Resources, imageFileName, string.Empty, true);
int fileSize = Request.Files[ "resourceIcon "].ContentLength;
Request.Files[ "resourceIcon "].SaveAs(savePath);
[解决办法]
public partial class UpLoadFile : System.Web.UI.Page
{
private string _JS = " <script language= 'javascript '> alert( '上传成功! ') </script> ";
protected void Page_Load(object sender, EventArgs e)
{
string s = this.Request.QueryString[ "type "].ToString();
string js = " <script language= 'javascript '> alert( '{0} '); </script> ";
string eJs = string.Format(js, s);
Response.Write(eJs);
}
protected void btn_upload_Click(object sender, EventArgs e)
{
try
{
if (fup_file.PostedFile.FileName.Length <= 0)
{
string sJS = " <script language= 'javascript '> alert( '请选择上传文件! ') </script> ";
Response.Write(sJS);
}
else
{
string sFilePath = fup_file.PostedFile.FileName;
string sFileName = sFilePath.Substring(sFilePath.LastIndexOf( "\\ ") + 1);
string sServerPath = Server.MapPath( ". ") + @ "\images\ "+sFileName;
fup_file.PostedFile.SaveAs(sServerPath);
Response.Write(_JS);
}
}
catch (Exception ex)
{
string sError = ex.Message;
string sJs = " <script language= 'javascript '> alert( '{0} ') </script> ";
string sMsg = string.Format(sJs, sError);
Response.Write(sMsg);
}
}