Issue #719Opened January 5, 2018by richieteh940 reactions

Respond problem for upload image

Question

Code SnippetTEXT
I know is an old problem but I have an issue for the respond where I tried to return a response by using asp.net to return a response
below is my code(services code to save into directory and return a response):
(code in ashx asp.net)
`           context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;
            try
            {
                HttpPostedFile postedFile = context.Request.Files[0] as HttpPostedFile;
                string tempPath = "/uploadImg";
                string savePath = context.Server.MapPath(tempPath);
                string filename = postedFile.FileName;
                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }
                string path = savePath + @"\" + filename;
                postedFile.SaveAs(path);

                Stream s = postedFile.InputStream;
                Image img = System.Drawing.Image.FromStream(s);

                int imgHeight = (int)img.PhysicalDimension.Height;
                int imgWidth = (int)img.PhysicalDimension.Width;

                TempModel tempModel = new TempModel();
                tempModel.type = "image";
                tempModel.src = "http://localhost:2002" + tempPath + "/" + filename;
                tempModel.height = imgHeight.ToString();
                tempModel.width = imgWidth.ToString();

                TempData tempData = new TempData();
                tempData.data = new List<TempModel>();
                tempData.data.Add(tempModel);


                string jsonString = JsonConvert.SerializeObject(tempData);
                string jsonRespond = jsonString.Insert(9,"\"" + "http://localhost:2002" + tempPath + "/\",");

                context.Response.Write(jsonRespond);
                context.Response.StatusCode = 200;



            }catch(Exception ex)
            {
                context.Response.Write("Error: " + ex.Message);
            }`
the code can save image to the directory but there is an error when parsing the response at grapesjs
what I facing is when i dropped my image the image couldn't automatic show in the panel.
I refered to the #138 but still having response problem

Answers (3)

artfJanuary 5, 20180 reactions

there is an error when parsing the response at grapesjs

Which error???

Probably just try to change context.Response.ContentType = "text/plain"; with context.Response.ContentType = "application/json";

richieteh94January 8, 20180 reactions

hi @artf the error is image I tried changed the context.Response.ContentType = "application/json"; but still get the above error and my response from service is as below image the response get by using Fiddler 4. and I would like to check whether the require response as above because it is weird that response from #138 where the one of the response does not have any key.

artfJanuary 8, 20180 reactions

Unexpected token E in JSON at position 0 means your response it's not a valid JSON string (which should start, for instance, with { or [ not E, probably there is an Error...). Check the response in your dev tools and you will see it

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.