文件错误如下所示,想做一个文件上传功能,这个功能模块是从另一个功能模块拷贝过来的,另一个功能模块能运行正常,不知道这个功能模块有哪写地方有问题。说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: 未能映射路径“/files/”。源错误: 行 45:                     string ty = File2.PostedFile.ContentType;
行 46:                    
行 47:                         File2.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(Pa) + ss);
行 48:                         s =  ss;
行 49:                         return "3"; 
源文件: f:\net计算机精品课程网站\YcCourseWeb\App_Code\upload.cs    行: 47 堆栈跟踪:
[InvalidOperationException: 未能映射路径“/files/”。]
   System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) +9781465
   System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPath virtualPath) +39
   System.Web.VirtualPath.MapPathInternal() +4
   System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) +110
   System.Web.HttpServerUtility.MapPath(String path) +104
   upload.Up(HtmlInputFile File2, String Pa) in f:\net计算机精品课程网站\YcCourseWeb\App_Code\upload.cs:47
   Admin_AddFile.Button1_Click(Object sender, EventArgs e) in f:\net计算机精品课程网站\YcCourseWeb\AddHomework.aspx.cs:26
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628614
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724文件源代码如下所示:upload.cs:using System;
using System.IO;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// upload 的摘要说明
/// </summary>
public class upload
{
public upload()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
    public string[] Resup ={ "上传失败或指定的文件不存在", "文件太大!", "格式不对,!", "上传成功!" };
        public string s = string.Empty;
    public string fname = string.Empty;
        public string Up(System.Web.UI.HtmlControls.HtmlInputFile File2, string Pa)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //            if (File2.PostedFile.ContentLength.ToString() == "0")
            {
                return "0";
            }
            else
            {
                //获取文件名称
                string ss;
                 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
                {
                    string ty = File2.PostedFile.ContentType;
                   
                        File2.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(Pa) + ss);
                        s =  ss;
                        return "3";
                        //Up= ss;
                
                }
            }        }
    }
AddHomework.aspx:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AddHomework.aspx.cs"  ValidateRequest="false" EnableEventValidation="false"  Inherits="Admin_AddFile" Title="Untitled Page" %>
<%@ Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
<asp:Content ID="Content1" runat="server"  contentplaceholderid="ContentPlaceHolder1">
    <table align="center" bgcolor="#e3ebfe" border="0" cellpadding="2" cellspacing="1"
            style="margin-top: 8px" width="98%">
                <tr>
               
               <td style="WIDTH: 3%; HEIGHT: 21px" class="td2" align="center">
                   作业名称:</td>
               <td style="HEIGHT: 21px" class="td2" colspan="3">
              <asp:TextBox ID="TextBox1" runat="server" CssClass="label" Width="558px"></asp:TextBox></td>
               
               </tr>
             <tr>
                 <td align="center" class="td2" style="width: 3%; height: 21px">
                     上传作业:</td>
                 <td align="left" class="td2" colspan="3" style="height: 21px">
                     <asp:TextBox ID="pic" runat="server" CssClass="inputBox" ReadOnly="True" Width="72px"></asp:TextBox><input
                         id="file1" runat="server" class="inputBox" style="width: 141px" type="file" /><asp:Button
                             ID="Button2" runat="server" CausesValidation="False" CssClass="formbutton" Height="22px"
                             OnClick="Button1_Click" Text="上传" Width="58px" /><asp:Label ID="Label1" runat="server"
                                 Visible="False" Width="60px"></asp:Label><input id="Hidden1" runat="server" type="hidden" /></td>
             </tr>
                 <tr>
           
               <td style="WIDTH: 3%; HEIGHT: 54px" class="td2" align="center">
                   内容说明:</td>
               <td style="WIDTH: 23%; HEIGHT: 54px" class="td2"  colspan="7">
                   <FCKeditorV2:FCKeditor ID="ArticleContent" runat="server" BasePath="~/FCKeditor/">
                       &nbsp;</FCKeditorV2:FCKeditor>
               </td>       
            
             </tr>
            <tr bgcolor="#e7e7e7">
                <td align="center" colspan="10" height="24">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="添加" /></td>
            </tr>
        </table> </asp:Content>AddHomework.aspx.cs:
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;public partial class Admin_AddFile : System.Web.UI.Page
{
    SqlHelper data = new SqlHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string res;
        upload up = new upload();
        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;
    }    protected void Button1_Click1(object sender, EventArgs e)
    {
        string filename = Hidden1.Value;
        int i = this.pic.Text.LastIndexOf(".");
        string newname = this.pic.Text.Substring(i);//获取文件的扩展名        string sql = "insert into Homework(TitleName,FileDs,FilePath,Ename,FileName)values('" + TextBox1.Text.Trim() + "','" + ArticleContent.Value + "','" + pic.Text + "','" + newname + "','" + filename + "')";
        data.RunSql(sql);        Alert.AlertAndRedirect("添加成功继续添加", "Homework.aspx");
    }
}

解决方案 »

  1.   

    当前项目路径下有files目录吗?
      

  2.   

    res = up.Up(file1, "../files/");
    System.Web.HttpContext.Current.Server.MapPath(Pa)你已经取了MapPath,又传入的../files/
    你这是要把文件存到网站文件夹外面的节奏吗?不要把文件存到网站文件夹外面
    你只应该将文件放到网站目录里面如果就是有需要把文件放到网站目录外面,那么你需要添加虚拟目录,把那个目录加进网站目录里来
      

  3.   

    MapPath获取的是网站根目录的路径
    跟你当前页面路径无关的用了MapPath,就别在用相对路径的..了,直接拼接绝对路径就好
      

  4.   

    要么你别用MapPath,就用相对路径不要混用
      

  5.   

     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);