if (this.FileUpload1.PostedFile.ContentLength == 0)
{
this.lblError.Text = "上传失败或文件不存在";
}
else
{
string[] Temp = this.FileUpload1.PostedFile.FileName.Split('\\');
FullFileName = this.FileUpload1.PostedFile.FileName;
string FileName = Temp[Temp.Length - 1];
//文件上传的路径
string PathName = Depart; //Depart是已经定义好的服务器上的目标文件夹
//如果文件上传的文件夹不存在,则直接创建一个文件夹
if (!Directory.Exists(PathName))
{
Directory.CreateDirectory(PathName);
}
this.FileUpload1.PostedFile.SaveAs(PathName + "\\" + FileName);
this.lblError.Text = "文件成功保存到服务器的" + PathName + "文件夹中";
将程序发布到服务器上之后,运行提示出错:未能找到路径“C:\Documents and Settings\dongli\桌面\car.doc”的一部分。
我觉得可能是我的文件路径是本地路径,而在服务器上是找的服务器路径,怎么解决这一问题,谢谢
14 个解决方案
#1
中文
#2
不知道
#3
PostedFile 一般不用这个属性....估计是这个弄错的..我给个我的代码..
#4
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;
public partial class TempFile_Default : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["type"] = Convert.ToInt32(Request.QueryString["type"].ToString());
if (Convert.ToInt32(Request.QueryString["type"].ToString()) == 2)
{
fileName.Attributes.Add("onchange", "window.parent.document.getElementById('flvprocess').style.display='block';this.form.submit()");
}
else
{
fileName.Attributes.Add("onchange", "window.parent.document.getElementById('imgprocess').style.display='block';this.form.submit()");
}
}
if (IsPostBack) save();
}
private void save()
{
bool flg = false;
if (fileName.HasFile)
{
string extension = Path.GetExtension(fileName.FileName).ToLower();
if (Regex.IsMatch(extension, @"^\.(?:jpge?|gif|png|flv)$"))
{
if ((Convert.ToInt32(ViewState["type"]) == 2 && extension == ".flv") || Convert.ToInt32(ViewState["type"]) == 1)
{
string rootpath = "TempFile";
string folderpath = Path.Combine(rootpath, DateTime.Now.ToString("yyyyMMdd"));
string nowpath = Path.Combine(Server.MapPath("~"), folderpath);
string filename = string.Empty;
Directory.CreateDirectory(nowpath);
do
{
filename = Guid.NewGuid().ToString() + extension;
}
while (File.Exists(Path.Combine(nowpath, filename)));
string virtualpath = (Path.Combine(folderpath, filename)).Replace("\\", "/");
string serverpath = Path.Combine(nowpath, filename);
fileName.SaveAs(serverpath);
Response.Write("<script type=\"text/javascript\">try{frameElement.callback(1,'" + virtualpath + "')}catch(exp){}</script>");
flg = true;
}
}
}
if (!flg) Response.Write("<script type=\"text/javascript\">try{frameElement.callback(0,'')}catch(exp){}</script>");
}
}
#5
fileName 就是fileupload控件....这段代码的命名貌似不太好,哈哈
#6
谢谢楼上的,我先试试,下班了,下午上班再看啊,谢谢,如果有什么问题继续问您呢
#7
private void LinkButton1_click(object sender,system.EVENTaRGS E)
{
HttpPostFile hpf=File2.PostedFile;
String fanem=hpf.FileName;
String f_name=fname.SubString(fname.LastINdexOf("\\")+1);
hpf.SaveAs(Server.MapPath("photo")+"\\"+Path.GetFileNmae(hpf.FileName));
sqlconnection con=new slconnection("server=.;uid=sa;pwd=;database=photolist");
con.Open();
sqlcommand cmd=new slqcommand ("insert inot photo (Title,description,photoname)values('"+title.text+"','"+descriptontext+"','"+f_name+"')",con);
cmd.executenonquery();
con.cloes();
}
{
HttpPostFile hpf=File2.PostedFile;
String fanem=hpf.FileName;
String f_name=fname.SubString(fname.LastINdexOf("\\")+1);
hpf.SaveAs(Server.MapPath("photo")+"\\"+Path.GetFileNmae(hpf.FileName));
sqlconnection con=new slconnection("server=.;uid=sa;pwd=;database=photolist");
con.Open();
sqlcommand cmd=new slqcommand ("insert inot photo (Title,description,photoname)values('"+title.text+"','"+descriptontext+"','"+f_name+"')",con);
cmd.executenonquery();
con.cloes();
}
#8
Server.MapPath一下,同时查一下MSDN帮助文件。
#9
楼上的,在哪用Server.MapPath阿
#10
是,用这个好点。
#11
string[] Temp = this.FileUpload1.PostedFile.FileName.Split('\\');
FullFileName = this.FileUpload1.PostedFile.FileName;
string FileName = Temp[Temp.Length - 1];
===============
你取文件名的方法有问题,改成这样:
string FileName = System.IO.Path.GetFileName(this.FileUpload1.PostedFile.FileName);
==============================
www.webdiyer.com
FullFileName = this.FileUpload1.PostedFile.FileName;
string FileName = Temp[Temp.Length - 1];
===============
你取文件名的方法有问题,改成这样:
string FileName = System.IO.Path.GetFileName(this.FileUpload1.PostedFile.FileName);
==============================
www.webdiyer.com
#12
楼主问这个问题的时候,你自己有没有先上网找过资料,网上的关于上传代码很多。等你找了,也许就找到答案了。
#13
查过了,有的看不懂,本人水平不是很高,呵呵,刚上班,刚果来,测试一下楼上几位的结果
#14
string path = Server.MapPath("~/虚拟路径");//获取本地磁盘物理路径
fu.PostFile.SaveAs(path);
fu.PostFile.SaveAs(path);
#1
中文
#2
不知道
#3
PostedFile 一般不用这个属性....估计是这个弄错的..我给个我的代码..
#4
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;
public partial class TempFile_Default : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["type"] = Convert.ToInt32(Request.QueryString["type"].ToString());
if (Convert.ToInt32(Request.QueryString["type"].ToString()) == 2)
{
fileName.Attributes.Add("onchange", "window.parent.document.getElementById('flvprocess').style.display='block';this.form.submit()");
}
else
{
fileName.Attributes.Add("onchange", "window.parent.document.getElementById('imgprocess').style.display='block';this.form.submit()");
}
}
if (IsPostBack) save();
}
private void save()
{
bool flg = false;
if (fileName.HasFile)
{
string extension = Path.GetExtension(fileName.FileName).ToLower();
if (Regex.IsMatch(extension, @"^\.(?:jpge?|gif|png|flv)$"))
{
if ((Convert.ToInt32(ViewState["type"]) == 2 && extension == ".flv") || Convert.ToInt32(ViewState["type"]) == 1)
{
string rootpath = "TempFile";
string folderpath = Path.Combine(rootpath, DateTime.Now.ToString("yyyyMMdd"));
string nowpath = Path.Combine(Server.MapPath("~"), folderpath);
string filename = string.Empty;
Directory.CreateDirectory(nowpath);
do
{
filename = Guid.NewGuid().ToString() + extension;
}
while (File.Exists(Path.Combine(nowpath, filename)));
string virtualpath = (Path.Combine(folderpath, filename)).Replace("\\", "/");
string serverpath = Path.Combine(nowpath, filename);
fileName.SaveAs(serverpath);
Response.Write("<script type=\"text/javascript\">try{frameElement.callback(1,'" + virtualpath + "')}catch(exp){}</script>");
flg = true;
}
}
}
if (!flg) Response.Write("<script type=\"text/javascript\">try{frameElement.callback(0,'')}catch(exp){}</script>");
}
}
#5
fileName 就是fileupload控件....这段代码的命名貌似不太好,哈哈
#6
谢谢楼上的,我先试试,下班了,下午上班再看啊,谢谢,如果有什么问题继续问您呢
#7
private void LinkButton1_click(object sender,system.EVENTaRGS E)
{
HttpPostFile hpf=File2.PostedFile;
String fanem=hpf.FileName;
String f_name=fname.SubString(fname.LastINdexOf("\\")+1);
hpf.SaveAs(Server.MapPath("photo")+"\\"+Path.GetFileNmae(hpf.FileName));
sqlconnection con=new slconnection("server=.;uid=sa;pwd=;database=photolist");
con.Open();
sqlcommand cmd=new slqcommand ("insert inot photo (Title,description,photoname)values('"+title.text+"','"+descriptontext+"','"+f_name+"')",con);
cmd.executenonquery();
con.cloes();
}
{
HttpPostFile hpf=File2.PostedFile;
String fanem=hpf.FileName;
String f_name=fname.SubString(fname.LastINdexOf("\\")+1);
hpf.SaveAs(Server.MapPath("photo")+"\\"+Path.GetFileNmae(hpf.FileName));
sqlconnection con=new slconnection("server=.;uid=sa;pwd=;database=photolist");
con.Open();
sqlcommand cmd=new slqcommand ("insert inot photo (Title,description,photoname)values('"+title.text+"','"+descriptontext+"','"+f_name+"')",con);
cmd.executenonquery();
con.cloes();
}
#8
Server.MapPath一下,同时查一下MSDN帮助文件。
#9
楼上的,在哪用Server.MapPath阿
#10
是,用这个好点。
#11
string[] Temp = this.FileUpload1.PostedFile.FileName.Split('\\');
FullFileName = this.FileUpload1.PostedFile.FileName;
string FileName = Temp[Temp.Length - 1];
===============
你取文件名的方法有问题,改成这样:
string FileName = System.IO.Path.GetFileName(this.FileUpload1.PostedFile.FileName);
==============================
www.webdiyer.com
FullFileName = this.FileUpload1.PostedFile.FileName;
string FileName = Temp[Temp.Length - 1];
===============
你取文件名的方法有问题,改成这样:
string FileName = System.IO.Path.GetFileName(this.FileUpload1.PostedFile.FileName);
==============================
www.webdiyer.com
#12
楼主问这个问题的时候,你自己有没有先上网找过资料,网上的关于上传代码很多。等你找了,也许就找到答案了。
#13
查过了,有的看不懂,本人水平不是很高,呵呵,刚上班,刚果来,测试一下楼上几位的结果
#14
string path = Server.MapPath("~/虚拟路径");//获取本地磁盘物理路径
fu.PostFile.SaveAs(path);
fu.PostFile.SaveAs(path);