帮我看一下,为什么我上传文件到虚拟服务器会找不到路径啊

富贵连连 2010-09-20 10:58:15
int fileSize=this.upfile.PostedFile.ContentLength;//得到上传文件大小
string name=this.upfile.PostedFile.FileName;//文件名
string type=name.Substring(name.LastIndexOf(".")+1);
FileStream fs=File.OpenRead(name);
byte[] content=new byte[fs.Length];
fs.Read(content,0,content.Length);
fs.Close();
if (type == "txt" || type == "sql" )
{
string filenname = "sql";
if(!Directory.Exists(Server.MapPath("~/file/")))
{
Directory.CreateDirectory(Server.MapPath("~/file/"));

}
string filepath = HttpContext.Current.Request.MapPath("~/file/" + filenname + "." + type);//当前路径
this.Label2.Text = filepath;
//判断是否有文件存在
if(System.IO.File.Exists(filepath))
{

File.Delete(filepath);
}
this.upfile.PostedFile.SaveAs(filepath);
...全文
137 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gongsun 2010-09-20
  • 打赏
  • 举报
回复
FileStream fs=File.OpenRead(name);

你本地测试的时候,自己机器就是服务器。

wuyq11 2010-09-20
  • 打赏
  • 举报
回复
File.OpenRead应该先上传再使用
(fileUpload.HasFile)
{
string savePath = Server.MapPath("~/upload/");
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);//保存文件
}
MSDNXGH 2010-09-20
  • 打赏
  • 举报
回复
多余的不说了,楼主看我代码~~
string path = Server.MapPath("~/tcimg/img/");

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (FileUpload1.HasFile)
{
this.FileUpload1.SaveAs(path + this.FileUpload1.FileName);
绝对地址 string ip= path + this.FileUpload1.FileName;
}
cpp2017 2010-09-20
  • 打赏
  • 举报
回复
FileStream fs=File.OpenRead(name);

==>
你的这个name是客户端的path

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧