-1

这是我的代码:

我只使用一个简单的数据列表来显示图像表单文件夹......

单击数据列表中的损坏图像图标后显示错误找不到资源。

说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的 URL:/WebSite2/Images/Desert.jpg

protected void BindDataList()
{
    DirectoryInfo dir = new DirectoryInfo(MapPath(@"~/Images/"+Model.Text+"/"));
    FileInfo[] files = dir.GetFiles();
    ArrayList listItems = new ArrayList();
    foreach (FileInfo info in files)
    {
        listItems.Add(info);
    }
    DataList1.DataSource = listItems;
    DataList1.DataBind();
}

protected void upload_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(Server.MapPath("Images/"+Model.Text+"/" + filename));
    BindDataList();
}


protected void newfolder_Click(object sender, EventArgs e)
{
    string NewDirectory = Server.MapPath("Images/"+Model.Text);
    //New Directory Name in string variable
    CreateDirectoryIfNotExists(NewDirectory);
    //Calling the function to create new directory
}

private void CreateDirectoryIfNotExists(string NewDirectory)
{
    try
    {
        // Checking the existance of directory
        if (!Directory.Exists(NewDirectory))
        {
            //If No any such directory then creates the new one
            Directory.CreateDirectory(NewDirectory);
            Label1.Text = "Directory Created";
        }
        else
        {
            Label1.Text = "Directory Exist";
        }
    }
    catch (IOException _err)
    {
        Label1.Text = _err.Message; ;
    }
}
4

2 回答 2

0

所有路径定义都应植根于~/

protected void upload_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(Server.MapPath("~/Images/"+Model.Text+"/" + filename));
    BindDataList();
}

protected void newfolder_Click(object sender, EventArgs e)
{
    string NewDirectory = Server.MapPath("~/Images/"+Model.Text);
    //New Directory Name in string variable
    CreateDirectoryIfNotExists(NewDirectory);
    //Calling the function to create new directory
}
于 2013-04-25T06:37:33.113 回答
0

试试这个:

protected void BindDataList()
{
    var imgDir = HttpContext.Current.Server.MapPath(string.Format(@"~/Images/{0}/", Model.Text));
    DirectoryInfo dir = new DirectoryInfo(imgDir);
    FileInfo[] files = dir.GetFiles();
    ArrayList listItems = new ArrayList();
    foreach (FileInfo info in files)
    {
        listItems.Add(info);
    }
    DataList1.DataSource = listItems;
    DataList1.DataBind();
}

protected void upload_Click(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    var imgPath = HttpContext.Current.Server.MapPath(string.Format(@"~/Images/{0}/{1}", Model.Text, filename));

    FileUpload1.SaveAs(imgPath);
    BindDataList();
}

protected void newfolder_Click(object sender, EventArgs e)
{
    var imgDir = HttpContext.Current.Server.MapPath(string.Format(@"~/Images/{0}/", Model.Text));
    //New Directory Name in string variable
    CreateDirectoryIfNotExists(imgDir);
    //Calling the function to create new directory
}
于 2013-04-25T06:38:26.647 回答