ASP.NET webAPI <--> Dojo uploader response f

2019-08-23 20:21发布

问题:

i have a problem with uploading documents with dojo. I´m using the dojox/form/Uploader to upload a file withiframe mode. After uploading there is a response from the server in a special format. The uploader only works when i send it like this:

Server: '{"name":"file.png","file":"filename","type":"form-data"}'

But the answer is: Client: "'{\"name\":\"file.png\",\"file\":\"filename\",\"type\":\"form-data\"}'"

So i think there are two possible ways to solve this problem:

A. Fixing it on the client side B. Fixing it on the server side

I could not find a solution for B. I´m using ASP.NET webAPI and create the reponse like that:

Dim msg = "<textarea>'{" + Chr(34) + "name" + Chr(34) + ":" + Chr(34) + fileName + Chr(34) + "," + Chr(34) + "file" + Chr(34) + ":" + Chr(34) + returnData.Item1 + Chr(34) + "," + Chr(34) + "type" + Chr(34) + ":" + Chr(34) + returnData.Item3 + Chr(34) + "}'</textarea>"
Dim response = Request.CreateResponse(Of String)(HttpStatusCode.Created, msg)
response.Content.Headers.ContentType = New MediaTypeHeaderValue("text/html")
Return response

Looks weird but the answer has to be enclosed by a html tag, the content is json.

Maybe there is a way to tell webAPI not to put those extra slashes into the response ? I gave up and decided to do it on the client side.

Problem is, i had to change the iframe.js file in the dojo library which is very bad.

回答1:

I solved the problem by myself, here is the server side solution:

json = System.Web.HttpUtility.HtmlEncode(json)
Dim msg = "<textarea>" + json + "</textarea>"

The dojo file uploader now understands the response and can parse it correctly.