I have to call javascript function from javascript file codebehind .aspx page . Presently I am using this syntax which is giving me an error.
this.Page.ClientScript.RegisterClientScriptInclude
("showalert('invalidusername','password')","/Public/JS/FailLogin.js");
You're calling the right method but as Guffa says, you're passing it invalid parameters.
Try something like this instead:
this.Page.ClientScript.RegisterClientScriptInclude("myKey",
"/Public/JS/FailLogin.js");
Or if you want inline script:
this.Page.ClientScript.RegisterClientScriptBlock(GetType(),
"myKey", "alert('whatever')");
Or to pass in some more dynamic script:
string name = "Joe";
string script = "alert('Your name is" + name + "')";
this.Page.ClientScript.RegisterClientScriptBlock(GetType(),
"myKey", script);
Please note that in the last example you most JavaScript encode the value of the "name" field. Depending on the version of .NET, one way to do it is this:
string encodedName = JavaScriptSerialize.Serialize(name);
And then pass the encoded name to the "script" variable.
You can even call both methods if you want to both include a JS file as well as run some code that depends on the newly included JS file (the script include should be rendered before the script block).
Related
So, as I figured out, when I have a form with enctype="multipart/form-data" and I upload a file, I can no longer access the object request. The following error is shown:
Cannot use the generic Request collection after calling BinaryRead.
After checking some resources, I stumpled upon a statement, which says: "This is by design". Well, okay, not here to judge about design-decisions.
To give you a quick overview, let me walk you through the code:
if request("todo") = "add" then
Set Form = New ASPForm
category = request("category")
title = request("title")
if len(Form("upload_file").FileName) > 0 then
filename = Form("upload_file").FileName
DestinationPath = Server.mapPath("personal/allrounder/dokumente/")
Form.Files.Save DestinationPath
end if
end if
Nothing too special here so far. Later however, when I try to access my request object, the error mentioned above occures:
<% if request("todo") = "new" then %>
...
My question now, how to get rid of it or fix this. I don't want to open the upload in a popup if there is another way around. This is the only solution I could think off.
Perfectly would be an object, which checks Form and request. Alternatively maybe a check at the top of the file, which object I have to use?
Thanks for any suggestions.
There used to be a very popular ASP class/component that solved ASP file uploads. The site for that component has been taken down, but the code is mirrored here:
https://github.com/romuloalves/free-asp-upload
You can include this ASP page on your own page, and on your page instantiate the class to get access to the files in your form, but also to the form variables. Here is a piece of example code (Upload.Form accesses the form fields):
Dim uploadsDir : uploadsDir = server.mapPath(".") ' whatever you want
Dim Upload, ks, fileKey, mailto
Set Upload = New FreeASPUpload
call Upload.Save(uploadsDir)
ks = Upload.UploadedFiles.keys
for each fileKey in ks
Response.write(fileKey & " : " & Upload.UploadedFiles(fileKey).FileName & "<br/>")
next
mailto = Upload.form("mailTo")
Set Upload = Nothing
If you want to stick to your own implementation, you can probably figure out how to get to the form variables in a multipart/form-data encoded data stream by having a look at the code they use to do so.
In my asp.net application, I am writing a file in code behind. I then want to use this file as below into a Handler but I get an error 'Illegal characters in Path'. I can't understand why? Help please.
The value of files in below is "306963020170816111848_Generic_P.pdf" and the file definitely exists in the correct path
WebClient client = new WebClient();
client.DownloadString(#"Handlers/MyPrintPdf.ashx?PdfFile=" + Server.MapPath("~/Templates/MyFiles/" + files)); // error here
Changed to use
HttpUtility.UrlEncode(#"Handlers/MyPrintPdf.ashx?PdfFile=" + Server.MapPath("~/Templates/MyFiles/" + files));
You should use the Uri overload of your DownloadString method. These parameters must be url encoded.
EDIT:
HttpUtility.UrlEncode(url)should also work.
In my Flex page I have a link that navigates to different webpage and I want to control the size of that child webpage. For resizing, I am using JavaScript command within my ActionScript. Here is the ActionScript code:
private function openLinkEvent():void{
var baseUrl:String ="https://localhost:8080/someWebsite?customerName="customer.custName;
var jscommand:String ="window.open(baseUrl,'win','height=280,width=500,toolbar=no,scrollbars=no,resizable=no');";
var url:URLRequest = new URLRequest("javascript:" + jscommand + " void(0);");
navigateToURL(url, "_self");
}
I have 2 issues in this code:
If I do it with above mentioned way, new window is not opening and Flex page is showing error "baseUrl is undefined". Why I am not able to pass variable for my url instead of passing the url directly in window.open method?
If I directly pass the value of my baseURL("https://localhost:8080/someWebsite?customerName="customer.custName;) in window.open, it is taking value of customerName as customer.custName while it should take the real value of a customer saved in the session.
Just to clarify, when I pass baseUrl in following way, my application is working absolutely fine.
navigateToURL(new URLRequest(baseUrl))
But above command is not sufficient if I want to re-size the browser window. That is why I am using JS command integrated with ActionScript.
If I do it with above mentioned way, new window is not opening and
flex page is showing error "baseUrl is undefined". Why I am not able
to pass variable for my url instead of passing the url directly in
window.open method?
Well, baseUrl the variable is defined, however you are not using when creating your jsCommand. You are using a string entitled baseUrl; not the varibale.
This is what you have:
var jscommand:String ="window.open(baseUrl,'win','height=280,width=500,toolbar=no,scrollbars=no,resizable=no');";
This is probably what you want:
var jscommand:String ="window.open('" + baseUrl + "','win','height=280,width=500,toolbar=no,scrollbars=no,resizable=no');";
Use string concatenation to create your jscommand string. so that the resulting value for jscommand is this:
window.open('https://localhost:8080/someWebsite?customerName=something','win','height=280,width=500,toolbar=no,scrollbars=no,resizable=no');
Instead of this:
window.open(baseUrl,'win','height=280,width=500,toolbar=no,scrollbars=no,resizable=no');
Hopefully that makes sense.
If I directly pass the value of my
baseURL("https://localhost:8080/someWebsite?customerName="customer.custName;)
in window.open, it is taking value of customerName as
customer.custName while it should take the real value of a customer
saved in the session.
Well, it is odd that you're missing the concatenation operator when creating your baseURL. Add the + between your string and the customer.custName:
var baseUrl:String ="https://localhost:8080/someWebsite?customerName=" + customer.custName;
I'm surprised that line was not giving a compile time error. However, since you have not elaborated on what type of class customer is, nor what custName means, it's hard to give a definitive on what is going on here. I assume that customer is an instance of a custom class you built and that custName is string property on it.
I am using Ext.js4 and Java servlets. When the user clicks a button on a panel, I execute a Servlet which in turn executes another application, which produces a log file in .txt format. I know the servlet and the other application are being executed. I want the servlet to return the contents of the .txt file, which Ext.js should in turn display as straight text on a separate "Log" tab. Do I need a store and model for something like this? Note that the data returned by the servlet is just text, not JSON or HTML, and special characters (which would preclude the use of JSON or HTML) may be included in that text. Thanks in advance.
No, you don't need a store and model for this.
I would write a servlet that simply returns the contents of the .txt file.
Then use the Ext.Ajax class to create a request to this servlet and save the text as a variable.
Then you can just do: logTab.update(theText) to fill your tab with the contents of the .txt file (where logTab is your tab you want updated).
For example:
Ext.Ajax.request({
url: '../textServlet',
success: function(response){
var theText = response.responseText;
logTab.update(theText); // or however you define your tab
}
});
I'm breaking my head over this for a while now and I have no clue what I do wrong.
The scenario is as followed, I'm using swfupload to upload files with a progressbar
via a webservice. the webservice needs to return the name of the generated thumbnail.
This all goes well and though i prefer to get the returned data in json (might change it later in the swfupload js files) the default xml data is fine too.
So when an upload completes the webservice returns the following xml as expected (note I removed the namespace in webservice):
<?xml version="1.0" encoding="utf-8"?>
<string>myfile.jpg</string>
Now I want to parse this result with jquery and thought the following would do it:
var xml = response;
alert($(xml).find("string").text());
But I cannot get the string value. I've tried lots of combinations (.html(), .innerhtml(), response.find("string").text() but nothing seems to work. This is my first time trying to parse xml via jquery so maybe I'm doing something fundemantally wrong. The 'response' is populated with the xml.
I hope someone can help me with this.
Thanks for your time.
Kind regards,
Mark
I think $(xml) is looking for a dom object with a selector that matches the string value of XML, so I guess it's coming back null or empty?
The First Plugin mentioned below xmldom looks pretty good, but if your returned XML really is as simply as your example above, a bit of string parsing might be quicker, something like:
var start = xml.indexOf('<string>') + 8;
var end = xml.indexOf('</string>');
var resultstring = xml.substring(start, end);
From this answer to this question: How to query an XML string via DOM in jQuery
Quote:
There are a 2 ways to approach this.
Convert the XML string to DOM, parse it using this plugin or follow this tutorial
Convert the XML to JSON using this plugin.
jQuery cannot parse XML. If you pass a string full of XML content into the $ function it will typically try to parse it as HTML instead using standard innerHTML. If you really need to parse a string full of XML you will need browser-specific and not-globally-supported methods like new DOMParser and the XMLDOM ActiveXObject, or a plugin that wraps them.
But you almost never need to do this, since an XMLHttpRequest should return a fully-parsed XML DOM in the responseXML property. If your web service is correctly setting a Content-Type response header to tell the browser that what's coming back is XML, then the data argument to your callback function should be an XML Document object and not a string. In that case you should be able to use your example with find() and text() without problems.
If the server-side does not return an XML Content-Type header and you're unable to fix that, you can pass the option type: 'xml' in the ajax settings as an override.