0

I have a folder which contains several PDF files and inserting and viewing all these pdf's are dont by 2 methods. I have to move this folder to place this folder outside of the project file. Therefore I have to use the absolute path. I have tried some coeds in the internet but none of them worked for me.

Following code is in a button clcik event

string directoryPath = @"D:\competion\pdfFolder\";
string svrPath = Server.MapPath(directoryPath);

DataSet ds = new DataSet();
string extension = Path.GetExtension(FileUpload1.FileName);
if ((FileUpload1.HasFile))
{
    if (extension == ".pdf")
    {
        if (grdPolicyDetails.Rows.Count > 0)
        {
             //Few methods are invoked in the body 
        }
    }
}

There are else parts in the if else statements but i haven't added those codes.

Mike
  • 1,017
  • 4
  • 19
  • 34
  • 2
    If you have the absolute path, why do you need to map it? – Inbar Barkai Sep 22 '16 at 05:58
  • I think in IIS you can make that folder path a virtual folder and add it (link it) to your site. (all the folders in your site don't necessarily have to be in your root folder). – nocturns2 Sep 22 '16 at 06:30
  • @nocturns2 It's great if you can provide a solution for this. – Mike Sep 22 '16 at 06:33
  • @user6592730 I've added a solution below. In IIS just right-click on the site, then select Add Virtual Directories – nocturns2 Sep 22 '16 at 07:47

4 Answers4

2

The Server.MapPath method works only with relative paths that are part of the web application structure. If you need to serve PDF files that are located outside, you might need to have some endpoint that will read the file contents on the server and stream it to the client.

For example:

string pdfPath = @"D:\competion\pdfFolder\myfile.pdf";
this.Response.ContentType = "application/pdf";
this.Response.TransmitFile(pdfPath);
this.Response.End();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I tried changing this code as i want and still it give the same exception saying "Path is not a valid virtual path" – Mike Sep 22 '16 at 06:05
  • also file name is combined later after few lines. – Mike Sep 22 '16 at 06:06
  • It's giving you this exception because you are trying to pass it to the `Server.MapPath` method. As you can see from my answer this doesn't work. As explained if you want to serve a file that is located outside of the directory structure of your application you will need to have a handler that will stream its contents to the response. – Darin Dimitrov Sep 22 '16 at 06:08
  • Ok, i will try this code completely and let you know – Mike Sep 22 '16 at 06:12
  • There are few things that i dont know how to change. So if i upload my whole code can you edit the code? It's a big help – Mike Sep 22 '16 at 06:14
  • I have added the complete code necessary to the task. Please take look at it. – Mike Sep 22 '16 at 06:22
1

I created a test project without the folder 'testVirPath'.

In my iis, I added a Virtual Directory that pointed to a folder (testVirPath) on a different drive (other than my project or published drive). I added the necessary permissions and the same user as my published site on the localhost.

I then added some pdf files to the testVirPath folder and published the project to iis. Try it. This will list the pdf files stored on the testVirPath folder.

[Home Controller]

    public ActionResult Files()
    {
        ViewBag.TheFiles = GetFiles(Server.MapPath("/testVirPath/"));
        return View();
    }

    private FileInfo[] GetFiles(string path)
    {
        DirectoryInfo di = new DirectoryInfo(path);
        FileInfo[] files = di.GetFiles();

        return files;
    }

[Files View]

<div>
    @foreach (FileInfo f in @ViewBag.TheFiles)
    {
        <p>@f.FullName</p>
    }
</div>
nocturns2
  • 663
  • 10
  • 17
0
string directoryPath = Server.MapPath("~/competion/pdfFolder/");

try this !!

G.Mich
  • 1,607
  • 2
  • 24
  • 42
  • I dont think server map method wors for files in local hard drives – Mike Sep 22 '16 at 08:12
  • work everywhere and if solution run on localhost or run on server. Symbol "~" take the root of your solution and add directories path next. – G.Mich Sep 22 '16 at 08:14
  • check this for more details about server map path http://stackoverflow.com/a/275791/5350526 – G.Mich Sep 22 '16 at 08:24
-1

Kindly Use these code for pdf upload.

if (fuDoc.HasFile) {

        string ext = "";
        string fnm = Path.GetFileName(fuDoc.PostedFile.FileName).ToLower();
        ext = Path.GetExtension(fuDoc.PostedFile.FileName).ToLower();
        if ((ext != ".doc") & (ext != ".pdf") & (ext != ".docx"))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Please select .doc or .pdf or .docx  files only');", true);
            fuDoc.Focus();
            return;
        }

        fuDoc.PostedFile.SaveAs(Server.MapPath("~/Upload/Documents/") + fuDoc.FileName);
        strDoc = "Upload/Documents/" + fuDoc.FileName;


    }
Snack'Eyes
  • 125
  • 7
  • Thanks for the code but i have solved the problem with uploading the pdf, now i have to display the pdf. Also i can't use Server.MapPath for this as the solution. – Mike Sep 22 '16 at 07:07
  • ohk then put "~" + "Your Path" on any button – Snack'Eyes Sep 22 '16 at 07:10
  • You can try this,Server.MapPath(“.”) - Returns the current physical directory of the page that are running. Server.MapPath(“..”) – Returns the parent physical directory of the page that are running. Server.MapPath(“~”) - Returns the physical path of the root directory of the application. Response.write(Server.MapPath(“../..”)+”
    ”) ;
    – Snack'Eyes Sep 22 '16 at 07:21
  • I used it ealier. It works without any error, but i have to create a folder outside of the project file. So i cant use this server map path it give error message when i use this method – Mike Sep 22 '16 at 07:23