[C# ASP.NET] Configuration to allow file save from javascrip

Example requests & Code samples for GdPicture Toolkits.
Post Reply
arghanoah
Posts: 4
Joined: Fri May 30, 2014 5:16 pm

[C# ASP.NET] Configuration to allow file save from javascrip

Post by arghanoah » Tue Jun 03, 2014 3:35 pm

Let me start with this preface... I know that this isn't a GdPicture issue but I'm hoping that somebody has done this before

I have been playing around with the ASP.NET sample project "Annotations" for several days now and am very impressed with it. I'm 99% done with my proof of concept for a project at work but I'm stuck with actually trying to "write" the file to disk. I am using the below javascript code, which came with the annotation sample project, with a couple of minor tweaks...

Code: Select all

function SaveAsPdfServer() {
            var oGdViewer = document.getElementById("GdViewer1");
            var FileName = GetNewGUID() + ".pdf";

            var URI = "http://example.com/Annotation/Upload/" + FileName;
            var UserName = "";
            var Password = "";

            var status = oGdViewer.SaveDocumentToPDF_2(URI, UserName, Password);

            if (status == 0) {
                alert("OK");
            }
            else {
                alert("error saving file. status: " + status);
            }
        }
The issue I am having is that the SaveDocumentToPDF_2(...) function completes with the "OK" alert box but there's no file. Behind the scenes it is issuing an HTTP PUT command to my web server. Using Fiddler to examine that tcp traffic I can see that the file is included with the sent Request based upon the content length.

Image

The server even replies with a Response code of 200 (everything OK) except that the file is not saved.

Image

I have added the necessary NTFS permissions for the application user to be able to write to the upload directory as well as added an additional HTTP Handler to allow "*.pdf" with the PUT verb to that specific upload directory. Since there are no errors to follow I'm stuck....

Has anyone used this functionality before and if so, what configuration did you use to allow the uploading of these files?

Thanks!

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by Loïc » Tue Jun 03, 2014 7:00 pm

Hi,
as well as added an additional HTTP Handler to allow "*.pdf" with the PUT verb to that specific upload directory.
Could you clarify what you've done to setup this handler?

Kind regards,

Loïc

arghanoah
Posts: 4
Joined: Fri May 30, 2014 5:16 pm

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by arghanoah » Tue Jun 03, 2014 8:40 pm

This is the web.config file for the upload folder, which is where I have edited the handlers.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers accessPolicy="Write">
            <remove name="StaticFile" />
            <remove name="WebDAV" />
            <add name="PUTVerbHandler" path="*.pdf" verb="PUT" modules="ProtocolSupportModule" resourceType="Unspecified" requireAccess="Write" />
        </handlers>
    </system.webServer>
</configuration>
Here are the file system permissions for that same directory.

Image

EDIT: I just noticed that there is a "remove" tag for the WebDAV handler, and WebDAV has actually been uninstalled from the server all together.

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by Loïc » Tue Jun 03, 2014 8:53 pm

OK. I have no experience in webdav so I will forward the thread to a specialist of our team. My guess is you should use a dedicated asp.net page with a handler to intercept the packets.
We will start investigations tomorrow and will let you know the result. In case you find something before our feedback us please let us know :)

With best regards,

Loïc

arghanoah
Posts: 4
Joined: Fri May 30, 2014 5:16 pm

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by arghanoah » Tue Jun 03, 2014 8:55 pm

Everything that I have found thus far has said to remove the WebDAV module from the web server because it intercepts those straight HTTP PUT verbs if you are trying to use RESTful design on the server side.

Thanks for taking a look at this, I will definitely post an answer if I ever find one that works.

Thanks,

Andy

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by Loïc » Tue Jun 03, 2014 9:00 pm

ok, we stay in touch. I'm quite sure we will have a solution by tomorrow.

arghanoah
Posts: 4
Joined: Fri May 30, 2014 5:16 pm

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by arghanoah » Tue Jun 03, 2014 10:20 pm

I GOT IT!!!!

I had to write a quick custom handler page to do it. Keep in mind this has no error handling in it yet, I was just trying to figure out how to do it.

Code: Select all

public void ProcessRequest (HttpContext context) {
        using (System.IO.Stream Input = context.Request.InputStream)
        {
            String Root = context.Server.MapPath("~/Upload/");
            Byte[] FileBuffer = new Byte[(Int32)Input.Length];
            using (System.IO.Stream Output = System.IO.File.OpenWrite(Root + Guid.NewGuid().ToString() + ".pdf"))
            {
                Input.Read(FileBuffer, 0, (Int32)Input.Length);
                Output.Write(FileBuffer, 0, (Int32)Input.Length);
            }
        }
    }
Here is the javascript implementation

Code: Select all

function SaveAsPdfServer() {
            var oGdViewer = document.getElementById("GdViewer1");
            var URI = "http://example.com/Annotation/Handler.ashx";
            var UserName = "";
            var Password = "";
            
            var status = oGdViewer.SaveDocumentToPDF_2(URI, UserName, Password);

            if (status == 0) {
                alert("OK");
            }
            else {
                alert("error saving file. status: " + status);
            }
        }
I didn't change any of the handlers that I had previously posted nor have I changed permissions on that upload directory. Weird, but I don't really care as I can finally get going again on this project!

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: [C# ASP.NET] Configuration to allow file save from javas

Post by Loïc » Tue Jun 03, 2014 10:30 pm

ok perfect so. I was thinking about something like that :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest