问题描述
文件错误如下所示,想做一个文件上传功能,这个功能模块是从另一个功能模块拷贝过来的,另一个功能模块能运行正常,不知道这个功能模块有哪写地方有问题。说明:执行当前Web请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.InvalidOperationException:未能映射路径“/files/”。源错误:行45:stringty=File2.PostedFile.ContentType;行46:行47:File2.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(Pa)+ss);行48:s=ss;行49:return"3";
源文件:f:net计算机精品课程网站YcCourseWebApp_Codeupload.cs行:47堆栈跟踪:[InvalidOperationException:未能映射路径“/files/”。]System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPathvirtualPath,BooleanpermitNull)+9781465System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPathvirtualPath)+39System.Web.VirtualPath.MapPathInternal()+4System.Web.HttpRequest.MapPath(VirtualPathvirtualPath,VirtualPathbaseVirtualDir,BooleanallowCrossAppMapping)+110System.Web.HttpServerUtility.MapPath(Stringpath)+104upload.Up(HtmlInputFileFile2,StringPa)inf:net计算机精品课程网站YcCourseWebApp_Codeupload.cs:47Admin_AddFile.Button1_Click(Objectsender,EventArgse)inf:net计算机精品课程网站YcCourseWebAddHomework.aspx.cs:26System.Web.UI.WebControls.Button.OnClick(EventArgse)+9628614System.Web.UI.WebControls.Button.RaisePostBackEvent(StringeventArgument)+103System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(StringeventArgument)+10System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandlersourceControl,StringeventArgument)+13System.Web.UI.Page.RaisePostBackEvent(NameValueCollectionpostData)+35System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+1724文件源代码如下所示:upload.cs:usingSystem;usingSystem.IO;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;///<summary>///upload的摘要说明///</summary>publicclassupload{publicupload(){////TODO:在此处添加构造函数逻辑//}publicstring[]Resup={"上传失败或指定的文件不存在","文件太大!","格式不对,!","上传成功!"};publicstrings=string.Empty;publicstringfname=string.Empty;publicstringUp(System.Web.UI.HtmlControls.HtmlInputFileFile2,stringPa){////TODO:在此处添加构造函数逻辑//if(File2.PostedFile.ContentLength.ToString()=="0"){return"0";}else{//获取文件名称stringss;fname=System.DateTime.Now.ToString().Replace("-","").Replace("","").Replace(":","");ss=System.DateTime.Now.ToString().Replace("-","").Replace("","").Replace(":","")+Path.GetExtension(File2.PostedFile.FileName);if(File2.PostedFile.ContentLength/1024>10000000){return"1";}else{stringty=File2.PostedFile.ContentType;File2.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(Pa)+ss);s=ss;return"3";//Up=ss;}}}}
AddHomework.aspx:<%@PageLanguage="C#"MasterPageFile="~/MasterPage.master"AutoEventWireup="true"CodeFile="AddHomework.aspx.cs"ValidateRequest="false"EnableEventValidation="false"Inherits="Admin_AddFile"Title="UntitledPage"%><%@RegisterAssembly="FredCK.FCKeditorV2"Namespace="FredCK.FCKeditorV2"TagPrefix="FCKeditorV2"%><asp:ContentID="Content1"runat="server"contentplaceholderid="ContentPlaceHolder1"><tablealign="center"bgcolor="#e3ebfe"border="0"cellpadding="2"cellspacing="1"style="margin-top:8px"width="98%"><tr><tdstyle="WIDTH:3%;HEIGHT:21px"class="td2"align="center">作业名称:</td><tdstyle="HEIGHT:21px"class="td2"colspan="3"><asp:TextBoxID="TextBox1"runat="server"CssClass="label"Width="558px"></asp:TextBox></td></tr><tr><tdalign="center"class="td2"style="width:3%;height:21px">上传作业:</td><tdalign="left"class="td2"colspan="3"style="height:21px"><asp:TextBoxID="pic"runat="server"CssClass="inputBox"ReadOnly="True"Width="72px"></asp:TextBox><inputid="file1"runat="server"class="inputBox"style="width:141px"type="file"/><asp:ButtonID="Button2"runat="server"CausesValidation="False"CssClass="formbutton"Height="22px"OnClick="Button1_Click"Text="上传"Width="58px"/><asp:LabelID="Label1"runat="server"Visible="False"Width="60px"></asp:Label><inputid="Hidden1"runat="server"type="hidden"/></td></tr><tr><tdstyle="WIDTH:3%;HEIGHT:54px"class="td2"align="center">内容说明:</td><tdstyle="WIDTH:23%;HEIGHT:54px"class="td2"colspan="7"><FCKeditorV2:FCKeditorID="ArticleContent"runat="server"BasePath="~/FCKeditor/"> </FCKeditorV2:FCKeditor></td></tr><trbgcolor="#e7e7e7"><tdalign="center"colspan="10"height="24"><asp:ButtonID="Button1"runat="server"OnClick="Button1_Click1"Text="添加"/></td></tr></table></asp:Content>
AddHomework.aspx.cs:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclassAdmin_AddFile:System.Web.UI.Page{SqlHelperdata=newSqlHelper();protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){}}protectedvoidButton1_Click(objectsender,EventArgse){stringres;uploadup=newupload();res=up.Up(file1,"../files/");this.Label1.Visible=true;this.Label1.Text=up.Resup[Convert.ToInt32(res)];this.pic.Text=up.s;Hidden1.Value=up.fname;}protectedvoidButton1_Click1(objectsender,EventArgse){stringfilename=Hidden1.Value;inti=this.pic.Text.LastIndexOf(".");stringnewname=this.pic.Text.Substring(i);//获取文件的扩展名stringsql="insertintoHomework(TitleName,FileDs,FilePath,Ename,FileName)values('"+TextBox1.Text.Trim()+"','"+ArticleContent.Value+"','"+pic.Text+"','"+newname+"','"+filename+"')";data.RunSql(sql);Alert.AlertAndRedirect("添加成功继续添加","Homework.aspx");}}
解决方案
解决方案二:
当前项目路径下有files目录吗?
解决方案三:
你的虚拟路径对吗?或者没有这个文件夹吧!???
解决方案四:
引用1楼feiyun0112的回复:
当前项目路径下有files目录吗?
有的
解决方案五:
res=up.Up(file1,"../files/");System.Web.HttpContext.Current.Server.MapPath(Pa)你已经取了MapPath,又传入的../files/你这是要把文件存到网站文件夹外面的节奏吗?不要把文件存到网站文件夹外面你只应该将文件放到网站目录里面如果就是有需要把文件放到网站目录外面,那么你需要添加虚拟目录,把那个目录加进网站目录里来
解决方案六:
MapPath获取的是网站根目录的路径跟你当前页面路径无关的用了MapPath,就别在用相对路径的..了,直接拼接绝对路径就好
解决方案七:
要么你别用MapPath,就用相对路径不要混用
解决方案八:
引用2楼u010145097的回复:
你的虚拟路径对吗?或者没有这个文件夹吧!???
如何修改虚拟路径,虚拟路径方面不是很懂,刚学C#。
解决方案九:
ss=System.DateTime.Now.ToString().Replace("-","").Replace("","").Replace(":","")+Path.GetExtension(File2.PostedFile.FileName);Replace("-","")把这个减号改成“/”ss=System.DateTime.Now.ToString().Replace("/","").Replace("","").Replace(":","")+Path.GetExtension(File2.PostedFile.FileName);