i am trying to open a pdf file on prod server. i am using the following code.
i get an error message
The resource cannot be found.
but the file do exist. it is a .pdf file.
the file is under a directory pdf/warehouse/itemlist.pdf
is there any thing wrong with my code..?
can any one help please..
private void openPdfFile(string pathToOpen)
{
string strRequest = pathToOpen; //-- if something was passed to the file querystring
if (strRequest != "")
{
string path = strRequest;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
// write file to browser
Response.WriteFile(file.FullName);
Response.End();
}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}
else
{
//nothing in the URL as HTTP GET
Response.Write("Please provide a file to download.");
}
}
i'm guessing that your problem may lie in the value you are passing into your code.
you've not shown us the actual value of pathToOpen
private void openPdfFile(string pathToOpen)
Please very carefully check the value of pathToOpen
also, you can try an experiment by temporarily modifiy your method signature to use a real path:
private void openPdfFile(string " ... real path to some file ...")
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
i have been trying different things, i tried to track the error, but couldn't find why i can't read the file. i have spoken to the server provider, and they have given the full permission. i am using the same code on another application and it is workinf
fine
this is the code that i am using
private void printList(List<Item> list)
{
try
{
var doc1 = new Document();
string fontpath = Server.MapPath("font");
string path = Server.MapPath("pdf/warehouse");
string dateToday = System.DateTime.Now.ToString("dd.MM.yyyy");
int numberOfItems = list.Count;
string fileName = "/" + "itemlist" + ".pdf";
try
{
PdfWriter.GetInstance(doc1, new FileStream(path + fileName, FileMode.Create));
iTextSharp.text.Font fontArkiv;
iTextSharp.text.Font fontForText;
iTextSharp.text.Font fontForHeaderDataTable;
createFonts(fontpath, out fontArkiv, out fontForText, out fontForHeaderDataTable);
doc1.Open();
PdfPTable headerTable = createHeaderWearhouse(fontArkiv, dateToday, "Warehouse");
doc1.Add(headerTable);
int columnsToCreate = 5;
float[] widths = new float[] { 1.5f, 1.5f, 0.8f, 0.5f, 0.5f };
PdfPTable table = createDataTableWithHeaderForArkiv(columnsToCreate, widths);
PdfPCell cellH1 = createColumnHeaderDataCell("Item Name", fontForHeaderDataTable);
table.AddCell(cellH1);
PdfPCell cellH2 = createColumnHeaderDataCell("Category", fontForHeaderDataTable);
table.AddCell(cellH2);
PdfPCell cellH3 = createColumnHeaderDataCell("Quantum", fontForHeaderDataTable);
table.AddCell(cellH3);
PdfPCell cellH4 = createColumnHeaderDataCell("New", fontForHeaderDataTable);
table.AddCell(cellH4);
PdfPCell cellH5 = createColumnHeaderDataCell("Used", fontForHeaderDataTable);
table.AddCell(cellH5);
for (int i = 0; i < numberOfItems; i++)
{
Item item = list[i];
PdfPCell text1 = new PdfPCell(new Phrase(item.itemName, fontForText));
//Ensure that wrapping is on, otherwise Right to Left text will not display
text1.NoWrap = false;
//Add the cell to the table
table.AddCell(text1);
PdfPCell text2 = new PdfPCell(new Phrase(item.categoryName, fontForText));
text2.NoWrap = false;
table.AddCell(text2);
PdfPCell text3 = new PdfPCell(new Phrase(item.quantum.ToString(), fontForText));
text3.NoWrap = false;
table.AddCell(text3);
PdfPCell text4 = new PdfPCell(new Phrase(item.newItems.ToString(), fontForText));
text4.NoWrap = false;
table.AddCell(text4);
PdfPCell text5 = new PdfPCell(new Phrase(item.used.ToString(), fontForText));
text5.NoWrap = false;
table.AddCell(text5);
}
PdfPCell totalItems = new PdfPCell(new Phrase("Total Items In House", fontForText));
totalItems.NoWrap = false;
totalItems.Colspan = 2;
table.AddCell(totalItems);
PdfPCell totalItemsQuantum = new PdfPCell(new Phrase(numberOfItems.ToString(), fontForText));
totalItemsQuantum.NoWrap = false;
totalItemsQuantum.Colspan = 3;
table.AddCell(totalItemsQuantum);
doc1.Add(table);
string userName = (string)Session["userName"];
PdfPTable signaturTable = createSignatur(fontArkiv, userName, "Warehouse");
doc1.Add(signaturTable);
}
catch (DocumentException dex)
{
Response.Write(dex.Message);
}
catch (IOException ioex)
{
Response.Write(ioex.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
doc1.Close();
}
string pathToOpen = path + fileName;
/* use this code on local server
Process AcrobatReader = new Process();
AcrobatReader.StartInfo.FileName = pathToOpen;
AcrobatReader.Start(); */
// use the method below on server
openPdfFile(pathToOpen);
}
catch (Exception ex)
{
Response.Redirect("error.aspx", false);
}
}
private void openPdfFile(string pathToOpen)
{
string strRequest = pathToOpen; //-- if something was passed to the file querystring
if (strRequest != "")
{
//get absolute path of the file
// the file is passed like a web address /images/myimage.jpg
string path = strRequest; //get file object as FileInfo
System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server
if (file.Exists) //set appropriate headers
{
Response.Clear();
Response.AddHeader("Content-Disposition", "filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
// write file to browser
Response.WriteFile(file.FullName);
Response.End();
}
else
{
// if file does not exist
Response.Write("This file does not exist.");
}
}
else
{
//nothing in the URL as HTTP GET
Response.Write("Please provide a file to download.");
}
}
Kamiran
Member
224 Points
194 Posts
can't open pdf file.!
Nov 19, 2012 11:07 AM|LINK
hi.
i am trying to open a pdf file on prod server. i am using the following code.
i get an error message
The resource cannot be found.
but the file do exist. it is a .pdf file.
the file is under a directory pdf/warehouse/itemlist.pdf
is there any thing wrong with my code..?
can any one help please..
private void openPdfFile(string pathToOpen) { string strRequest = pathToOpen; //-- if something was passed to the file querystring if (strRequest != "") { string path = strRequest; System.IO.FileInfo file = new System.IO.FileInfo(path); if (file.Exists) //set appropriate headers { Response.Clear(); Response.AddHeader("Content-Disposition", "filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/pdf"; // write file to browser Response.WriteFile(file.FullName); Response.End(); } else { // if file does not exist Response.Write("This file does not exist."); } } else { //nothing in the URL as HTTP GET Response.Write("Please provide a file to download."); } }dhol.gaurav
Contributor
3998 Points
725 Posts
Re: can't open pdf file.!
Nov 19, 2012 11:33 AM|LINK
You have to pass full path into respose check below code
// Your Code Response.AddHeader("Content-Disposition", "filename=" + file.Name); // Replace with below code Response.AddHeader("Content-Disposition", "filename=" + path);let me know if any query
Gaurav Dhol
Skype ID : dhol.gaurav
If My Post contains helped you, Please Mark as Answer
Kamiran
Member
224 Points
194 Posts
Re: can't open pdf file.!
Nov 19, 2012 11:42 AM|LINK
@dhol.gaurav
i tried, not working.!
i have used the same code already on another application, and it is working fine..! now it is not..!
i have tried with my hosting provide,and they say that they can't find any thing wrong..
oned_gk
All-Star
31377 Points
6414 Posts
Re: can't open pdf file.!
Nov 19, 2012 12:01 PM|LINK
Kamiran
Member
224 Points
194 Posts
Re: can't open pdf file.!
Nov 19, 2012 04:01 PM|LINK
@oned_gk
the file name is itemlist.pdf, this is an alphabet file name.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: can't open pdf file.!
Nov 19, 2012 05:40 PM|LINK
@ Kamiran
i'm guessing that your problem may lie in the value you are passing into your code.
you've not shown us the actual value of pathToOpen
Please very carefully check the value of pathToOpen
also, you can try an experiment by temporarily modifiy your method signature to use a real path:
g.
Paul Linton
Star
13407 Points
2533 Posts
Re: can't open pdf file.!
Nov 19, 2012 08:30 PM|LINK
What acount does the web server process run under. Does that account have permission to read the file?
oned_gk
All-Star
31377 Points
6414 Posts
Re: can't open pdf file.!
Nov 19, 2012 09:49 PM|LINK
is pdf exist in mimetype list your iis? If not, add it to the list.
Is there any additional error message than not found message?
Kamiran
Member
224 Points
194 Posts
Re: can't open pdf file.!
Nov 21, 2012 05:44 AM|LINK
i have been trying different things, i tried to track the error, but couldn't find why i can't read the file. i have spoken to the server provider, and they have given the full permission. i am using the same code on another application and it is workinf fine
this is the code that i am using
private void printList(List<Item> list) { try { var doc1 = new Document(); string fontpath = Server.MapPath("font"); string path = Server.MapPath("pdf/warehouse"); string dateToday = System.DateTime.Now.ToString("dd.MM.yyyy"); int numberOfItems = list.Count; string fileName = "/" + "itemlist" + ".pdf"; try { PdfWriter.GetInstance(doc1, new FileStream(path + fileName, FileMode.Create)); iTextSharp.text.Font fontArkiv; iTextSharp.text.Font fontForText; iTextSharp.text.Font fontForHeaderDataTable; createFonts(fontpath, out fontArkiv, out fontForText, out fontForHeaderDataTable); doc1.Open(); PdfPTable headerTable = createHeaderWearhouse(fontArkiv, dateToday, "Warehouse"); doc1.Add(headerTable); int columnsToCreate = 5; float[] widths = new float[] { 1.5f, 1.5f, 0.8f, 0.5f, 0.5f }; PdfPTable table = createDataTableWithHeaderForArkiv(columnsToCreate, widths); PdfPCell cellH1 = createColumnHeaderDataCell("Item Name", fontForHeaderDataTable); table.AddCell(cellH1); PdfPCell cellH2 = createColumnHeaderDataCell("Category", fontForHeaderDataTable); table.AddCell(cellH2); PdfPCell cellH3 = createColumnHeaderDataCell("Quantum", fontForHeaderDataTable); table.AddCell(cellH3); PdfPCell cellH4 = createColumnHeaderDataCell("New", fontForHeaderDataTable); table.AddCell(cellH4); PdfPCell cellH5 = createColumnHeaderDataCell("Used", fontForHeaderDataTable); table.AddCell(cellH5); for (int i = 0; i < numberOfItems; i++) { Item item = list[i]; PdfPCell text1 = new PdfPCell(new Phrase(item.itemName, fontForText)); //Ensure that wrapping is on, otherwise Right to Left text will not display text1.NoWrap = false; //Add the cell to the table table.AddCell(text1); PdfPCell text2 = new PdfPCell(new Phrase(item.categoryName, fontForText)); text2.NoWrap = false; table.AddCell(text2); PdfPCell text3 = new PdfPCell(new Phrase(item.quantum.ToString(), fontForText)); text3.NoWrap = false; table.AddCell(text3); PdfPCell text4 = new PdfPCell(new Phrase(item.newItems.ToString(), fontForText)); text4.NoWrap = false; table.AddCell(text4); PdfPCell text5 = new PdfPCell(new Phrase(item.used.ToString(), fontForText)); text5.NoWrap = false; table.AddCell(text5); } PdfPCell totalItems = new PdfPCell(new Phrase("Total Items In House", fontForText)); totalItems.NoWrap = false; totalItems.Colspan = 2; table.AddCell(totalItems); PdfPCell totalItemsQuantum = new PdfPCell(new Phrase(numberOfItems.ToString(), fontForText)); totalItemsQuantum.NoWrap = false; totalItemsQuantum.Colspan = 3; table.AddCell(totalItemsQuantum); doc1.Add(table); string userName = (string)Session["userName"]; PdfPTable signaturTable = createSignatur(fontArkiv, userName, "Warehouse"); doc1.Add(signaturTable); } catch (DocumentException dex) { Response.Write(dex.Message); } catch (IOException ioex) { Response.Write(ioex.Message); } catch (Exception ex) { Response.Write(ex.Message); } finally { doc1.Close(); } string pathToOpen = path + fileName; /* use this code on local server Process AcrobatReader = new Process(); AcrobatReader.StartInfo.FileName = pathToOpen; AcrobatReader.Start(); */ // use the method below on server openPdfFile(pathToOpen); } catch (Exception ex) { Response.Redirect("error.aspx", false); } } private void openPdfFile(string pathToOpen) { string strRequest = pathToOpen; //-- if something was passed to the file querystring if (strRequest != "") { //get absolute path of the file // the file is passed like a web address /images/myimage.jpg string path = strRequest; //get file object as FileInfo System.IO.FileInfo file = new System.IO.FileInfo(path); //-- if the file exists on the server if (file.Exists) //set appropriate headers { Response.Clear(); Response.AddHeader("Content-Disposition", "filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/pdf"; // write file to browser Response.WriteFile(file.FullName); Response.End(); } else { // if file does not exist Response.Write("This file does not exist."); } } else { //nothing in the URL as HTTP GET Response.Write("Please provide a file to download."); } }Kamiran
Member
224 Points
194 Posts
Re: can't open pdf file.!
Nov 23, 2012 01:32 PM|LINK
It is working fine now, i guess is was a permission issue on the server.
after more investigation with the support it was suddenly working fien..
Thanks for the help..