Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
design
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fu2.aspx.cs" Inherits="fu2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:FileUpload ID="FileUpload1" runat="server" />

    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>





c#:
code

C#
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class fu2 : System.Web.UI.Page
{

    protected void Button1_Click(object sender, EventArgs e)
    {
        string filePath = "~/fileup/";
        string fileName = FileUpload1.FileName.ToString();
        FileUpload1.SaveAs(Server.MapPath(filePath) + fileName);
    }
}


i am getting this error when i want to upload pdf
A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll


but this error is not display when i upload image and txt files etc
Posted

1 solution

Try changing the way you save it slightly:
C#
FileUpload1.SaveAs(Server.MapPath(filePath) + fileName);
Becomes:
C#
FileUpload1.SaveAs(Server.MapPath(filePath + fileName));
The reason is that MapPath does not put a trailing '/' character on the path spec - so your file would be trying to save to
C:\mywebsite\fileupmypdffile.pdf
 
Share this answer
 
Comments
adilmemon 16-Oct-11 12:40pm    
it is not working same problem all files are uploaded except pdf
adilmemon 25-Oct-11 11:22am    
can anybody solve this problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900