How to call the server map path filename that generated randomly

This is a C# code for a Web Form page named "upload.aspx" which allows the user to upload a PDF file and store it in the server. The file uploaded must be a PDF file, and all the text boxes must be filled before submission. The file name is saved in the database as a string with the format of "GUID.pdf", Guid.NewGuid().ToString()
.
UPLOAD.ASPX.CS
tTR.FileName = Guid.NewGuid().ToString() + ".pdf";
dataBaseDataContext.TTRs.InsertOnSubmit(tTR);
dataBaseDataContext.SubmitChanges();
String path = Server.MapPath("Attachments/" + tTR.FileName);
FileUploadtoServer.SaveAs(path);
i created a search function that allows the user to search for based on Heat Code, Item Code, PO Number, and Description. user can also edit and download the file by clicking on the "Edit / Download" link. The problem here is in my Download button. It's not downloading/ able to read the path. i can see the pdf exist in the Attachment folder but it's not able to find the correct file name associated to the heat code.
public partial class track : System.Web.UI.Page
{
DataBaseDataContext dataBaseDataContext = new DataBaseDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
private void BindData(List<MTR> Data)
{
try
{
if (Data.Count > 0)
{
StringBuilder append = new StringBuilder();
foreach (MTR mTR in Data)
{
string PdfGuid = mTR.FileName;
append.Append("<tr><td><a style=\"color:blue;\" href=\"#\" onclick=\"doPop('" + mTR.id + "');\">Edit / Download</a></td><td>" + mTR.Heat_Code + "</td><td>" + mTR.Item_Code + "</td><td>" + mTR.PO_Number + "</td><td>" + mTR.Description + "</td><td>" + mTR.Invoice_No + "</td></tr>");
}
tdList.InnerHtml = append.ToString();
}
}
catch (Exception)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string filePath = Server.MapPath("~/Attachments/" + filename + ".pdf");
byte[] content = File.ReadAllBytes(filePath);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".pdf");
Response.Buffer = true;
Response.BinaryWrite(content);
Response.End();
}
catch (Exception)
{
}
The Button1_Click handler has a "filename" variable but there is no indication where or when this value is set. I assume wherever filename is set is the problem. Use the Visual Studio debugger to check the value of "filename" is correct given the filter criteria. Also get the "filePath" and verify the path actually exists.
There's some JavaScript too doPop('" + mTR.id + "') but there is no indication what doPop does.
Also, you have an empty catch which basically hides exceptions from yourself.
I save the filename like this when i upload . I'm not sure how to call the random generated filename in the download button. dpop list the details
I cannot trace what to input in the filepath and filename response.addheader here
You must have code that looks up the filename in the database. Usually, this is by an Id. Where is this code?
I search by heatcode or po-number or invoice number or description.
Here's the code that searches by heat code, item, PO and description. The comment section doesn't allow me to enter code, it keeps throwing me an error
Add the "~/" to the save logic.
it's already there in my code, byte[] content = File.ReadAllBytes(Server.MapPath("~/Attachments/"
The problem is when i add tTR.FileName, it says tTR does not exist in the context. I wonder what it's looking for
That is the wrong code! Update the code that saves the file to disk. Your current code looks like the following.
It should be the save as the code that fetches the file.
This was mentioned above as well. Hopefully, this will fix the issue. If not there is a problem with looking up the file name in the database.
@AgaveJoe I modified it in the upload code. The download button is calling an empty pdf file every time i click download on a record.
tTR.FileName = Guid.NewGuid().ToString() + ".pdf";
String path = Server.MapPath("~/Attachments/" + tTR.FileName);
looking at the above two lines of code for uploading i'm still not confident with the filename in the below script, coz it's downloading a blank pdf.
Hi @Lan Huang-MSFT , Thanks alot for the suggestion. However in my requirement uploading and downloading were done in two different pages upload.aspx and track.aspx.
i upload the file as i mentioned in my code above
mTR.FileName = Guid.NewGuid().ToString() + ".pdf";
String path = Server.MapPath("~/Attachments/" + mTR.FileName);
when i download the pdf, it downloads a blank pdf for every record
The latest code snippet does not return the file contents. I'm not sure why you removed these lines.
@AgaveJoe when i use the file content it says the
Hi @Sunny ,
An example I rewrote from your description, edited in the answer.
First, you need to make sure you got the correct FileName, you can check the breakpoint.
fileName already includes the pdf suffix, no need to add it.
Have you checked that the pdf in the Attachments folder is valid? You need to save to the server first, then to the database.
Sign in to comment
Hi @Sunny ,
How did you get your filename?
If the download is not successful, it should be that you have not obtained the correct filename.
You can refer to the example below. upload.aspx
track.aspx
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.
Sign in to comment
0 additional answers
Sort by: Most helpful
Activity