locked
Response.write with JavaScript in httpHandler RRS feed

  • Question

  • User51847363 posted

    Hi everyone, newbie here. Embarassed

    I am writing a upload handler with C# and it will respond with javascript after user uploads an image.

    The source code is as follows:

    <%@ WebHandler Language="C#" Class="UploadHandler" %>
    
    using System;
    using System.Web;
    using System.IO;
    using System.Web.SessionState;
    
    public class UploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState {
    
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpResponse response = context.Response;
    
                string uploadPath = "/upload/";
                HttpPostedFile uploads = context.Request.Files["upload"];
                string CKEditorFuncNum = context.Request["CKEditorFuncNum"];
                string file = System.IO.Path.GetFileName(uploads.FileName);
                string fileNameOnly = uploads.FileName;
                string extension = Path.GetExtension(file);
                String postDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                fileNameOnly = postDateTime + "_" + fileNameOnly;
                file = fileNameOnly + "." + extension;
                uploads.SaveAs(context.Server.MapPath("~") + uploadPath + fileNameOnly);
                string url = "/contentManager" + uploadPath + fileNameOnly;
                string errorMsg = "Error occurred.";
                HttpCookie cookie = new HttpCookie("uploadFileName",file);
                cookie.Path = "/contentManager/contentStore/upcomingEvents/";
                response.Cookies.Add(cookie);
                context.Response.Write("<script>window.parent.CKEDITOR.tools.callFunction("+ CKEditorFuncNum +", '" + url + "');</script>");
                context.Response.End();
        }
    
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }
    
    

    Actually, it works fine by returning the JavaScript. However, those javascript got printed out as usual text like this:

    "<script>window.parent.CKEDITOR.tools.callFunction(1, '/contentManager/upload/20161206154126_download.jpg');</script>"

    As you can see from the above code (viewed the source code), that script has a double quotes at the start and end.

    My question is, how do I avoid those quotes as it is the reason why the javascript is not interpreted as it should be.

    Cheers,

    Angelo

    Tuesday, December 6, 2016 7:51 AM

All replies

  • User-6180675 posted

    Try to change your contenttype to "application/x-javascript" as you will be returning only active javascript in the browser.

    context.Response.ContentType = "application/x-javascript";

    Also make sure that the page where you are calling the HTTP handler has access to the javascript function you are calling otherwise you will get null reference.

    let me know if it works

    Tuesday, December 6, 2016 8:16 AM