I was requested to add some searching functionality to an existing system for the collection of PDFs that we have. I know about searching PDFs and opening them with search parameters and in a test application I wrote, it works like a dream. When trying to convert it over to our existing application the PDF opens but without the search terms or the advanced find of Acrobat Reader popping up. Any help would be greatly appreciated!
Here is a snippet of the cs code :
case "PDF":
string searchTerms = SearchWordsTB.Text;
searchTerms = searchTerms.Replace(',', ' ');
launchStr = "OpenPDF('" + e.Row.Cells[9].Text.Replace("\\", "/") + "','" + HttpUtility.UrlEncode(e.Row.Cells[2].Text) + "','" + e.Row.Cells[0].Text + "','" + searchTerms + "')";
break;
We are creating the list of documents on the fly and PDF is one of the options. Assuming I am understanding this correctly, A DataGrid is created with all these clickable rows that will execute a Javascript function when clicked. The Javascript function OpenPDF is shown below:
function OpenPDF(url, filename, ID, searchTerms) {
if (searchTerms.length > 0) {
window.open('FileViewer.aspx?name=' + filename + '&ID=' + ID + '&url=' + url + '#search="' + searchTerms + '"', 'mywindow' + windowCnt, 'width=800,height=600,location=no,resizable=yes');
}
else {
window.open('FileViewer.aspx?name=' + filename + '&ID=' + ID + '&url=' + url, 'mywindow' + windowCnt, 'width=800,height=600,location=no,resizable=yes');
}
windowCnt++;
}
From following the debugging in the CS code, I know that I am properly stripping out the commas in the search terms so that shouldn't be the problem. What currently happens is the PDF file will open up just fine, but the search terms are not being used. I have tried following the debugger through the Javascript (which for me has always been spotty at best) but the breakpoint is never hit. It should also probably be noted that the Javascript function is kept in a separate Javascript File and is not inline in the aspx page. And yes, we are correctly referencing the Javascript file. I will be more than happy to update this post with any extra info that is requested. Thanks in advance for any help!
I was able to achieve the desired results by using the http encode on the launch string as shown below.
launchStr = "OpenFile('" + HttpUtility.UrlEncode(e.Row.Cells[9].Text.Replace("\\", "/") + "#search=\"" + searchTerms + "\"") + "','" + HttpUtility.UrlEncode(e.Row.Cells[2].Text) + "','" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "')";
I then used the function to just open the window with the PDF in it. The problem I had was that without the HTTP Encode, the URL was just cutting off the search parameters. I believe this is because the #search="blah" isn't normally recognized as part of a URL and was therefore truncated. If anyone has a better reason, I would love to hear it.
Related
After open pop window, i need to reload my page. here page gets reloaded but pop window not opened. How to solve it.
string url = "Popup.aspx";
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
Response.Redirect("~/mysamepage.aspx", false);
Have you tried a Reload in the javascript by appending it to the s string:
window.location.reload(false);
// If you need to fetch the webpage from the web-server again (where the page
//contents change dynamically) pass the argument as 'true' instead of 'false'.
eg
string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');window.location.reload(false);";
or try:
window.opener.href('url').reload(true);
I have a scenario where I loop through a data set (max of 6 records) and then open a new browser tab for each record - each tab shows an invoice for one of the records - not the best design but it's what was requested.
I'm using the code below within a foreach to build a url and open a new browser tab, the problem is it loops through ok but only ever opens 1 new tab.
Every other thing happening in the loop works so the problem seems to be with the code. It opens the first tab for the first record then no more after that.
Can anyone comment on what's wrong?
string pageurl = "Label.aspx?booking=" + v.booking + "&pallet=" + v.palletId;
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "window.open('" + pageurl + "','_blank')", true);
You can only have one startup script. Try putting all of your window.open calls in a single script;
//This code inside loop
string pageurl = "Label.aspx?booking=" + v.booking + "&pallet=" + v.palletId;
string script += "window.open('" + pageurl + "','_blank'); "
//This code outside loop
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", script, true);
string path = Path.GetDirectoryName(Application.ExecutablePath) +
"\\" + directores[directores.Length - 2] +
"\\" + Path.GetFileName(url_img).Replace(" ", "_");
client.DownloadFileAsync(new Uri(url_img), path);
This code over doesn't work, the path seems to be good when i check it via MessageBox:
I have tried also with # symbol, but it neither works.
If I enter not a path but a name, e.g
client.DownloadFileAsync(new Uri(url_img), "test");
everything is ok. How can I solve this?
Forgot to add: If such path doesn't exist, I want to create it!
Whenever using async methods is your responsibility to check the result. For DownloadFileAsync the responsibility is to implement a DownloadFileCompleted. In the event, look at the Error, it will give you precious clues about why it failed.
Found a solution:
if (!Directory.Exists(directores[directores.Length - 2]))
Directory.CreateDirectory(directores[directores.Length - 2]);
string path = Path.GetDirectoryName(Application.ExecutablePath) +
"\\" + directores[directores.Length - 2] +
"\\" + Path.GetFileName(url_img).Replace(" ", "_");
if (!Directory.Exists(directores[directores.Length - 2])) Directory.CreateDirectory(directores[directores.Length - 2]);
client.DownloadFileAsync(new Uri(url_img), path);
Thanks for trying help.
So this function doesn't create folder if it doesn't exist, we have to create it before.
I have a "error.aspx" page which is there to mail me if any exception is caught. When I open the page manually, mysite.com/error.aspx, the page opens fine but when it is redirected by a catch block with the exception.message and exception.stackTrace as querystrings, I get an error "page not found". Are the querystrings directing the browser to open a different url? It works fine when run on localhost, though.
public void send_error(Exception ex)
{
Response.Redirect("error.aspx?time=" + DateTime.Now.ToString() + "&ex=" + ex.Message + "&st=" + ex.StackTrace.Replace("\n", " "), false);
}
If you check this Article, you will see that the max query length of url string is 2048 symbols for Internet explorer. Probably the url is bigger and because of that you have this problem. One solution is to save the desire message in the session as string and after that retrieve it on other pages.
string errorMessage = DateTime.Now.ToString() + " " + ex.Message + " " + ex.StackTrace.Replace("\n", " ");
Session["__ErrMessage"] = errorMessage;
When you are in other pages you can access this string like this:
string errMessage = "";
if(Session["__ErrMessage"] != null)
errMessage = Session["ErrMessage"].ToString();
Can anyone help with this? I keep getting an error File beeing used by another process.
Here's my code so far:
string username = textBox1.Text;
string password = textBox2.Text;
string SecurityCode = textBox3.Text;
if(!Directory.Exists(path + "\\Login"))
Directory.CreateDirectory(path + "\\Login");
if (!File.Exists(path + "\\Login\\Username.omg" + "\\Login\\Password.omg" + "\\Login\\SecurityCode.omg"))
File.Create(path + "\\Login\\Username.omg");
File.Create(path + "\\Login\\Password.omg");
File.Create(path + "\\Login\\SecurityCode.omg");
File.AppendAllText(path + "\\Login\\Username.omg", username);
File.AppendAllText(path + "\\Login\\Password.omg", password);
File.AppendAllText(path + "\\Login\\SecurityCode.omg", SecurityCode);
MessageBox.Show("User Created: Welcome: " + username);
}
File.Create returns a Stream that you need to close before trying to access the file just created
using(File.Create(path + "\\Login\\Username.omg"))
;
using(File.Create(path + "\\Login\\Password.omg"))
;
using(File.Create(path + "\\Login\\SecurityCode.omg"))
;
a simple using statement around the File.Create call could help to close and dispose the returned stream
However, as stated in the comments above from Mr Hans Passant, your simple scenario could be served better by a simple call to File.WriteAllText()
File.WriteAllText(Path.Combine(path, #"login\username.omg"), username);
File.WriteAllText creates the file if it doesn't exist and overwrite its content if it exists. In your code, instead, you append the info to the same files over and over.
I am not sure that this is really what you want here.
By the way, your call to File.Exists is wrong. You should test each file separately
string userNameFile = Path.Combine(path, #"login\username.omg");
if (!File.Exists(userNameFile)
{
using(File.Create(userNameFile))
;
}