JavaScript to upload to server - c#

I am writing an ASP.NET script to capture a signature, I want to save the file on the server side.
The following code is JavaScript for saving the file. But it does not allow me to save from browser disable by safety measure:
flags = 0x2481000; //SigObj.renderRelative | SigObj.outputFilename | SigObj.color32BPP | SigObj.encodeData
rc = sigCtl.Signature.RenderBitmap(filename, -160, -160, "image/bmp", 0.5, 0xff0000, 0xffffff, 0.0, 0.0, flags );
How can I upload via JavaScript or C#?

If you do not find it to be overkill, for your uses, i suggest trying out https://github.com/blueimp/jQuery-File-Upload, i do think it is possible to adapt for use with C#. There is quite an extensive documentation for the plugin.
It is based on jQuery UI.
Cheers Kalle

Related

Is there a way to switch environments while using Jint.NET?

I'm currently using the Jint.NET JavaScript console engine for C#, and I'm attempting to emulate separate JavaScript environments that the user can switch between.
However, I don't want to create an entirely new console engine for each JavaScript environment and cause a large overhead, instead just switch between them using a single engine and store the environments somewhere else, eg:
engine 1
| |
| |
env 1 env 2
Is it possible to do this?
Found a solution to this, although there's no documentation for this anywhere that I can find, so this may be an unintended method. You need to use EnterExecutionContext on newly created LexicalEnvironments, and you can switch between them to have separate JS environments.
Here's an example:
using Jint.Runtime.Environments;
using Jint.Native.Object;
using Jint.Native.Global;
//Create a new object instance and environment.
JSObjectInstance = GlobalObject.CreateGlobalObject(jintEngine);
JSEnvironment = LexicalEnvironment.NewObjectEnvironment(jintEngine, JSObjectInstance, jintEngine.GlobalEnvironment, false);
//Enter the new environment.
jintEngine.EnterExecutionContext(JSEnvironment, JSEnvironment, new Jint.Native.JsValue(false));
And when you're done with that environment, you can leave using LeaveExecutionContext, and rejoin the default global one like so:
jintEngine.EnterExecutionContext(jintEngine.GlobalEnvironment, jintEngine.GlobalEnvironment, jintEngine.Global);

Enabling annotation in Adobe AxAcroPDFLib

I embedded a PDF viewer in a C# Winform using AxAcroPDFLib.
However, the annotation buttons in the toolbar (comments...) are disabled.
I searched and found that they are disabled by default, but some reported enabling them using Javascript:
Collab.showAnnotToolsWhenNoCollab = True
Is there a way to do this here?
Edit: Is it possible to use the browser plugin in a WebBrowser Control? If so, how can this be done?
Update - The first section is relevant only to Acrobat Reader. For information on when using full versions of Acrobat, see the second section.
Acrobat Reader
I'll preface all of this by stating this is probably not the answer you're looking for, but I felt this warranted more of an explanation than just a comment.
A similar, self-answered question was asked on SO (here), where the OP came to the conclusion that this behavior is by design and nothing cannot be done about it, which I agree with, almost.
While I'm sure you've seen that Reader itself can add annotations, the only straightforward means of accomplishing this using the Reader Plugin (AcroPDFLib) is for the document being loaded to be "Reader Enabled," at which point annotations become available just as they are in Reader. If you have control of the documents you wish the plugin to load, this may be a solution for you.
To your question about possibly setting Collab.showAnnotToolsWhenNoCollab = True as a workaround, my searches only showed this being a viable workaround for those using a full version of Acrobat, not Reader. More specifically, on an Adobe forum (here), an Adobe staff commented on the use of this property directly:
No, it is not [about allowing commenting in Adobe Reader]. It is
about enabling commenting in a browser for Acrobat Standard or
Professional. If you wish to enable commenting in Reader, then you
need to "Reader Enable" the PDFs themselves using Acrobat professional
or Adobe Livecycle Reader Extension Server.
Granted, this comment was in reference to Acrobat 9, it appears to still be valid for Acrobat XI.
One last bit. I don't know the scope of your application, so this may be completely irrelevant, but if this is a commercial application, even if do you find a functional workaround, I'd be hesitant to use it, as it might violation the Adobe Reader license agreement (here); specifically section 4.3.3, Disabled Features. The short version is, as with most companies, they don't want you circumventing their protections.
Full versions of Acrobat
The following code will create a PDF viewer (using the Form's window for drawing), open a PDF, then set collab.showAnnotToolsWhenNoCollab = true to allow annotations on the open PDF. This requires a reference to the Acrobat type library.
void CreatePdfViewerAndOpenFile(string pdfFile)
{
short AV_DOC_VIEW = 2;
short PDUseBookmarks = 3;
short AVZoomFitWidth = 2;
Type AcroExch_AVDoc = Type.GetTypeFromProgID("AcroExch.AVDoc");
_acroExchAVDoc = (Acrobat.CAcroAVDoc)Activator.CreateInstance(AcroExch_AVDoc);
bool ok = _acroExchAVDoc.OpenInWindowEx(pdfFile, this.Handle.ToInt32(), AV_DOC_VIEW, -1, 0, PDUseBookmarks, AVZoomFitWidth, 0, 0, 0);
if (ok)
{
CAcroPDDoc pdDoc = (CAcroPDDoc)_acroExchAVDoc.GetPDDoc();
object jsObj = pdDoc.GetJSObject();
Type jsObjType = jsObj.GetType();
object collab = jsObjType.InvokeMember("collab",
BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,
null, jsObj, null);
jsObjType.InvokeMember("showAnnotToolsWhenNoCollab",
BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance,
null, collab, new object[] { true });
}
}
Call this method from wherever you want to display the PDF. When finished, be sure to call the Close method or the PDF file will remain open in the Acrobat process in the background.
_acroExchAVDoc.Close(-1);
Bear in mind that a lot of "normal" functionality is left out of this example, like form resize handling, etc., but it should get you started. Because resizing isn't handled by this example, you'll probably want to maximize the form before invoking the method, so the viewer is big enough to be useful. For more information on how to use the viewer in this fashion, download the Acrobat SDK (here) and look at the ActiveViewVB sample project, which is what I used to build some of this example. For reference, I used the Acrobat XI SDK.

Loading a website inside WPF

I've been playing with loading a website (specifically my blog) inside a WebBrowser in WPF. My code is pretty much just one line:
WebBrowser.Source = new Uri("http://www.example.net");
When I run this I get scripting error, like this:
I get similar errors when running other web sites, although not always related to Google Ads.
My assumption is that there is an issue with sites that run JS. Is this correct and, if so, why? Is there a way around it?
The problem here is that the WPF WebBrowser did not implement this property as in the 2.0 control. But with reflection you can get to the activexinstance of the webbrowser control to put the browser in silent mode:
dynamic activeX = this.WB.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, this.WB, new object[] { });
activeX.Silent = true;

How to upload images using an API Key that gives you permission to upload? [Python source code included]

The documentation of the API shows source code on how to accomplish this in Python:
#!/usr/bin/python
import pycurl
c = pycurl.Curl()
values = [
("key", "YOUR_API_KEY"),
("image", (c.FORM_FILE, "file.png"))]
# OR: ("image", "http://example.com/example.jpg"))]
c.setopt(c.URL, "http://imgur.com/api/upload.xml")
c.setopt(c.HTTPPOST, values)
c.perform()
c.close()
I'd like a bit of guidance on how to do this in C#. For instance, I'm lost on what these "values" would be in C#, how would I even declare them?
I'm not familiar with cURL at all, so that might be holding me back from translating a bit.
Help me Obi-wan. You're my only hope. /click
You just need to perform an HTTP POST, e.g. this code with a "parameters" string of key=YOUR_API_KEY&image=http://example.com/example.jpg or the like.

Create HTML webpage programmatically in C#

I was wondering: is there a way to create HTML files programmatically in C# as you can do with XML? Mine is a console application, so maybe some of the options are not available. Basically, I would like to do something smarter than just building a big string.
Possible scenario:
Instead of writing:
string html="<html><head>Blah</head><body>{0}</html>", myotherstring
I would like to work as in XML
XmlTextWriter w = new XmlTextWriter(xml_file_path + xml_file_name,
System.Text.Encoding.UTF8);
w.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
// construct xml
XmlElement root = xmlDoc.CreateElement("element");
...
xmlDoc.Save(w);
w.Close();
Apologies for the naive question.
Don't forget: You can generate XHTML just as easily as plain XML using the XmlTextWriter approach.
You could use NVelocity. It is a .Net port of the Java Velocity templating system. The API will not be similar to XmlWriter. Instead, you'll write a text file in a simple scripting language, put your objects into a 'context' and then merge the template and the context to generate the HTML file.
NVelocity
You could use some third party open-source libraries to generated strong typed verified (X)HTML, such as CityLizard Framework or Sharp DOM.
For example
html
[head
[title["Title of the page"]]
[meta_(
content: "text/html;charset=UTF-8",
http_equiv: "Content-Type")
]
[link_(href: "css/style.css", rel: "stylesheet", type: "text/css")]
[script_(type: "text/javascript", src: "/JavaScript/jquery-1.4.2.min.js")]
]
[body
[div
[h1["Test Form to Test"]]
[form_(action: "post", id: "Form1")
[div
[label["Parameter"]]
[input_(type: "text", value: "Enter value")]
[input_(type: "submit", value: "Submit!")]
]
]
[div
[p["Textual description of the footer"]]
[a_(href: "http://google.com/")
[span["You can find us here"]]
]
[div["Another nested container"]]
]
]
];
I realise that this question is old, however the recent release of the ASP.Net MVC 3 Razor view engine now gives you the option to use this same Razor view engine to generate HTML for any purpose.
See Hosting Razor outside of ASP.Net for a guide on how to do this.
What I did a few months back, I had an asp.net file (aspx) saved as a template in a text file, whenever the user needed a new page, I would just copy that template into the user specified folder, change the extension .txt to .aspx, and programmatically add a few options depending on the user's needs. It was a simple page though. Of course, the more complex you go, the more complex the code will be.

Categories

Resources