MapPath error

Donald Symmons 2,856 Reputation points
2022-05-20T14:07:26.143+00:00

I created a site where I issue PDF or JPEG documents files to clients with a QR code on the documents. The issue I am facing is that when the QR code is scanned it displays a link with the document ID and when I navigate to the link it shows this:

Your file couldn’t be accessedIt may have been moved, edited, or deleted.
ERR_FILE_NOT_FOUND

But the file is inside the httpdocs folder in my Plesk. How do I resolve this?

Here is the link it displays on a QR code scanner
G:\PleskVhosts\docissuers.com\httpdocs\UIdoc.aspx?Id=3

here is the code used to generate the QR code

 private string imgBytes;
    private object Inv;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindCardAndQR();
    }

    private void BindCardAndQR()
    {
        if (Session["paperinv"] != null)
        {
            int id = Convert.ToInt32(Session["paperinv"]);

            string sql = "SELECT * FROM tableinvoice WHERE Id = @Id";
            SqlParameter[] parameters = new[] {
                    new SqlParameter("@Id", id)
                };

            // Bind Image3 for Card Image
            DataTable dt = SelectFromDatabase(sql, parameters);
            if (dt.Rows.Count > 0)
            {
                string embed = "<object data=\"{0}{1}\" type=\"application/pdf\"></object>";
                ltEmbed.Text = string.Format(embed, ResolveUrl("Handler.ashx?Id="), id);

                byte[] QRBytes = GetQRCodeBytes(Server.MapPath("~/IUdoc.aspx") + "?Id=" + id);
                Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
            }
        }
    }
    public byte[] GetQRCodeBytes(string url)
    {
        //QR code generated here
        QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap bi = encoder.Encode(url);
        MemoryStream tmpSteam = new MemoryStream();
        bi.Save(tmpSteam, ImageFormat.Jpeg);
        return tmpSteam.ToArray();
    }
    public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)
    {
        using (SqlConnection con = new SqlConnection("myconstring"))
        {

            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (parameters != null)
                {
                    cmd.Parameters.AddRange(parameters);
                }

                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,251 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,966 Reputation points
    2022-05-20T15:20:14.897+00:00

    File paths don't work from web apps. If that G:\Plex path is what you're returning to the browser then it won't work. The link you need to provide to the user is a URL that is valid for your site. You then need to write the server side code to respond to that URL, grab the file contents from whereever and download it to the browser. The user cannot access server-based file paths from the browser.

    It appears you're using Web Forms here. Is that correct? If so then you'll probably want to use an ASHX handler for this. In the answer to that question they demonstrate how to pass data to the handler. You'd need to pass something that uniquely identifies the file to download. On the server side you'd get the file based upon what the user gave to you in the query string. Once you've found the file you'd download it to the browser as that demo code is already doing. The only thing you'd need MapPath for is if you're using a virtual path but that probably doesn't make sense here.


2 additional answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,066 Reputation points
    2022-05-23T09:25:27.223+00:00

    Hi @Donald Symmons ,
    Instead of generating complete data from database to QRCode, generate a page that will be used to download the document . Then in the QRCode save the url. When user scan the QRCode then he will be redirected to the url which will generate the document.

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Donald Symmons 2,856 Reputation points
    2022-05-26T07:08:02.227+00:00

    Calling a Method worked

    public string ToAbsoluteUrl(string relativeUrl)
                {
                    if (string.IsNullOrEmpty(relativeUrl))
                    {
                        return relativeUrl;
                    }
                    if (relativeUrl.StartsWith("/"))
                    {
                        relativeUrl = relativeUrl.Insert(0, "~");
                    }
    
                    if (!relativeUrl.StartsWith("~/"))
                    {
                        relativeUrl = relativeUrl.Insert(0, "~/");
                    }
    
                    var url = HttpContext.Current.Request.Url;
                    var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
    
                    return String.Format("{0}://{1}{2}{3}", url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
                }