Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello friends,
I am working on a website where I am giving a facility to the user to download his resume.

The code is working well locally but on the server it is giving an error like:
:-Access to the path 'D:\WEBDATA\allaboutstaffingjobs.com\Test\JobSeeker\Resume\_Loginmastersors.txt' is denied.

This is my code:

C#
private void DownloadFile(string fname, bool forceDownload)
{
   // string path = MapPath(fname);
    string name = Path.GetFileName(fname);
    string ext = Path.GetExtension(fname);
    string type = "";
    // set known types based on file extension
    if (ext != null)
    {
        switch (ext.ToLower())
        {
            case ".htm":
            case ".html":
                type = "text/HTML";
                break;
            case ".txt":
                type = "text/plain";
                break;
            case ".doc":
            case ".rtf":
            case ".docx":
                type = "Application/msword";
                break;
        }
    }
    if (forceDownload)
    {
        Response.AppendHeader("content-disposition",
            "attachment; filename=" + name);
    }
    if (type != "")
        Response.ContentType = type;
    Response.WriteFile(fname);
    Response.End();
}

protected void linkdownload_Click(object sender, EventArgs e)
{
    objCandidate.IntJobseekerid = Convert.ToInt32(Session["JobSeekerId"]);
    dsCandidate = objCandidate.SelectJSProfileEduInfoByJobseekerId();
    if ((dsCandidate.Tables[0].Rows[0]["ResumeFile"]).ToString().Trim().Length > 0)
    {
           string DirPath;
          DirPath = Server.MapPath("./Resume/");
        string databasename=(dsCandidate.Tables[0].Rows[0]["ResumeFile"]).ToString();
        string  destinationfile=DirPath+databasename;
        string extnsn=(dsCandidate.Tables[0].Rows[0]["ResExtnsn"]).ToString();
        int Eduid=Convert.ToInt32(dsCandidate.Tables[0].Rows[0]["ProfileEduId"]);
        //string filename = dsCandidate.Tables[0].Rows[0]["ResumeFile"].ToString();
        string folderfile = "Resume" + Eduid.ToString() + extnsn;
     string sourcefile=DirPath+folderfile;
     if (File.Exists(destinationfile) == true)
     {
         File.Delete(destinationfile);
     }
     if (File.Exists(sourcefile) == true)
     {
         File.Copy(sourcefile, destinationfile);
         string filename = databasename;
         //uploadresume.SaveAs(Path.Combine(DirPath, "Resume" + i.ToString() + Path.GetExtension(uploadresume.FileName).ToLower()));
         DownloadFile(DirPath + filename, true);
         File.Delete(destinationfile);
    }
    }
}


I am not getting where the problem is, please help me.

Thanks in advance.
Posted
Updated 17-Jun-11 20:53pm
v2

Your basic issue is that you don't understand how ASP.NET works. ASP.NET has NO access to any filesystem above it's root. You need to store the files inside your application folders, you can't just store them to a random place in the file system.

So, you're OK with two people uploading a resume that happens to have the same filename, and then the first user has their resume deleted, and they find they have access to the data that belongs to the second person ? Have you considered storing these things in a database, and using the user information to make sure that each person's data is secure ?
 
Share this answer
 
It is probably a permssions issue: you need to give the correct permssions to the folder where you intend to store the documents.

CG is also correct in that you can't just put all the doucmnets in the folder and hope a) that you never get a name collision or b) that there are so many documents in ther that the whole thing just grinds to a halt.

You should rename each file with a guid and store that and the original file name in the database. You can also create a new folder for groups of files using the first 5 or 6 characters of the guid and store matching files.

Further, as CG says, you need to ensure that the storage folder is either within your application or is properly permissioned and accessible.
 
Share this answer
 
Comments
pawanvats 18-Jun-11 3:02am    
storage folder is in my application
R. Giskard Reventlov 18-Jun-11 3:08am    
and did you bother to check permssions or do you expect one of us to come to your office and do it for you?
R. Giskard Reventlov 18-Jun-11 3:23am    
Use reply - no need to add a new comment.
I gave you an answer: not my fault if you choose to ignore it.

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