asp.net 上传一个很奇怪的问题

nadongjiao4 2013-08-07 11:28:35
各位大哥好,小弟最近在做项目的时候,碰上了一个很奇怪的问题,问题是这样的:我在本机映射一个盘符,我想把上传的文件保存到映射的盘符里面,在本机里面测试是可以通过,部署到服务器里面测试,就不能上传文件,各位大哥,帮我看看是什么问题,求详解。。

下面是上传测试代码:

// 不限制大小
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
UploadInfo uploadInfo = this.Session["UploadInfo"] as UploadInfo;
uploadInfo.IsReady = true;

string myDicPath = string.Empty;
string path = string.Empty;
Boolean hasFile = fileUpload.HasFile;
Session["hasFile"] = hasFile;
if (this.fileUpload.PostedFile != null && this.fileUpload.PostedFile.ContentLength > 0)
{
string fileName = Path.GetFileName(this.fileUpload.PostedFile.FileName);
string filepath=fileUpload.PostedFile.FileName.ToString();
string Type = filepath.Substring(filepath.LastIndexOf(".") + 1);
if (Type == "doc" || Type == "pdf" || Type == "txt" || Type == "chm" || Type == "ppt" || Type == "sql" || Type == "xls" || Type == "et" || Type == "jnt" || Type == "dps")
{
path = this.Server.MapPath(@"UpLoadFile\\帮助文档"); //文件在服务器上的路径
GetFileType(Type);

}else if(Type=="exe"){
path = this.Server.MapPath(@"UpLoadFile\\安装程序文件");
GetFileType(Type);
}
else if (Type == "rar" || Type == "zip")
{
path = this.Server.MapPath(@"UpLoadFile\\升级包文件");
GetFileType(Type);
}
else if (Type == "dll")
{
path = this.Server.MapPath(@"UpLoadFile\\程序更新文件");
GetFileType(Type);
}
else {
path = this.Server.MapPath(@"UpLoadFile\\其他文件");
GetFileType(Type);
}

//Session 存上传文件名
//Session["fileName"] = fileName.ToString();
Session["Path"] = path.ToString();
uploadInfo.ContentLength = this.fileUpload.PostedFile.ContentLength;

Session["fileLength"] = uploadInfo.ContentLength;
uploadInfo.FileName = fileName;
uploadInfo.UploadedLength = 0;

//uploadInfo.IsReady = true; //告诉服务器,已经准备好了,

int bufferSize = 1;
byte[] buffer = new byte[bufferSize];

using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
while (uploadInfo.UploadedLength < uploadInfo.ContentLength)
{
int bytes = this.fileUpload.PostedFile.InputStream.Read(buffer, 0, bufferSize);
fs.Write(buffer, 0, bytes);
uploadInfo.UploadedLength += bytes;
}
}
//上传成功标识
const string js = "window.parent.onComplete('success', '系统已成功上传附件: {0}');";
ScriptManager.RegisterStartupScript(this, typeof(upload_aspx), "progress", string.Format(js, fileName), true);
}
else
{
const string js = "window.parent.onComplete('error', '上传文件出错');";
ScriptManager.RegisterStartupScript(this, typeof(upload_aspx), "progress", js, true);
}
uploadInfo.IsReady = false;
}
}
//获取文件类型
public void GetFileType(string type)
{
string myDicPath = string.Empty;
string filePath = fileUpload.PostedFile.FileName.ToString();
Session["fileName"] = filePath.ToString();
string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);
string fileDirectory = Session["fileDirectory"].ToString();//数据库文件总目录
if (type == "doc" || type == "pdf" || type == "txt" || type == "ppt" || type == "chm" || type == "sql" || type == "xls" || type == "et" || type == "jnt" || type == "dps")
{
myDicPath = fileDirectory+"\\帮助文档";
if (!Directory.Exists(myDicPath))
Directory.CreateDirectory(myDicPath);
fileUpload.PostedFile.SaveAs(myDicPath +"\\"+ fileName); //文件保存在磁盘上
}
else if (type == "exe")
{
myDicPath = fileDirectory + "\\安装程序文件";
if (!Directory.Exists(myDicPath))
Directory.CreateDirectory(myDicPath);
fileUpload.PostedFile.SaveAs(myDicPath + "\\" + fileName);
}
else if( type=="rar" || type == "zip")
{
myDicPath = fileDirectory + "\\升级包文件";
if (!Directory.Exists(myDicPath))
Directory.CreateDirectory(myDicPath);
fileUpload.PostedFile.SaveAs(myDicPath + "\\" + fileName);
}
else if (type == "dll")
{
myDicPath = fileDirectory + "\\程序更新文件";
if (!Directory.Exists(myDicPath))
Directory.CreateDirectory(myDicPath);
fileUpload.PostedFile.SaveAs(myDicPath + "\\" + fileName);
}
else {
myDicPath = fileDirectory + "\\其他文件";

if (!Directory.Exists(myDicPath))
Directory.CreateDirectory(myDicPath);
fileUpload.PostedFile.SaveAs(myDicPath + "\\" + fileName);
}

}
</script>
...全文
213 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nadongjiao4 2013-08-08
  • 打赏
  • 举报
回复
引用 8 楼 insus 的回复:
嗯,是的,有点小小问题,不过,Insus.NET已经更改正确了,参考: http://www.cnblogs.com/insus/p/3243143.html
您好,谢谢你这么热心的帮我决解问题,刚刚引用了你给的例子,测试了一下,还是存在问题,问题是:除了其他文件可以找到相对应的文件夹以外,不管上传什么样的的后缀(里面规定的后缀名),都归类在帮助文档里面,例如,上传后缀名为DLL,也归类到帮助文档里面。希望能得到你热心的解答,谢谢您,大哥,您辛苦了。
吕津 2013-08-08
  • 打赏
  • 举报
回复
nadongjiao4 2013-08-08
  • 打赏
  • 举报
回复
引用 3 楼 yaotomo 的回复:
提示什么错误?
提示:HTTP 500内部服务器错误,这个是什么原因造成的呢。。
hjx398 2013-08-07
  • 打赏
  • 举报
回复
会不会使权限的问题?
insus 2013-08-07
  • 打赏
  • 举报
回复
看看效果:
insus 2013-08-07
  • 打赏
  • 举报
回复
嗯,是的,有点小小问题,不过,Insus.NET已经更改正确了,参考: http://www.cnblogs.com/insus/p/3243143.html
nadongjiao4 2013-08-07
  • 打赏
  • 举报
回复
引用 5 楼 nadongjiao4 的回复:
[quote=引用 4 楼 insus 的回复:] Insus.NET对你最后一个方法,改写成这个样子: 建议先理清代码,然后才可以方便找出问题所在: http://www.cnblogs.com/insus/p/3243143.html
谢谢你,改了之后,问题还是一样。[/quote] 引用了你修改之后的代码,每次传过来的后缀得到的都是“其他文件夹”而得不到相对应的目录哦。可以给我一个完整的方案吗?
insus 2013-08-07
  • 打赏
  • 举报
回复
先参考一个简单的例子,放在服务器,能否把文件上传,如果成功之后,再做文件类型判断: http://www.cnblogs.com/insus/p/3208744.html 我们上传时,往往需要做很多判断,由于在本机测试时,本机即是服务器,服务器也是本机,因此一切都没有什么异常出现。 如果把程序上传至服务器时,上传文件有先后顺序,建议确保文件真正上传至服务器之后,再作文件类型,大小等判断。 因此,当用户选择文件时,点上传,第一个步骤是先存储一个临时目录,然后再对它进行判断。 一切都判断通过,再从临时目录移至或拷贝至真正目录。 更多fileupload: http://www.cnblogs.com/insus/tag/FileUpload/
nadongjiao4 2013-08-07
  • 打赏
  • 举报
回复
引用 4 楼 insus 的回复:
Insus.NET对你最后一个方法,改写成这个样子: 建议先理清代码,然后才可以方便找出问题所在: http://www.cnblogs.com/insus/p/3243143.html
谢谢你,改了之后,问题还是一样。
insus 2013-08-07
  • 打赏
  • 举报
回复
Insus.NET对你最后一个方法,改写成这个样子:



建议先理清代码,然后才可以方便找出问题所在:
http://www.cnblogs.com/insus/p/3243143.html

yaotomo 2013-08-07
  • 打赏
  • 举报
回复
提示什么错误?
nadongjiao4 2013-08-07
  • 打赏
  • 举报
回复
引用 1 楼 hjx398 的回复:
会不会使权限的问题?
不会,在映射文件里面我已经设置好了,可以读,可以写的权限。

62,266

社区成员

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

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

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

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