1

I have a simple webservice that I would like to upload a file to. The problem is that I need the response in json.

Form my experience in order to get a response in Json my request has to have a content-type of 'application/json'. But ofcourse this cannot be the case with a file upload since the content type will have to be 'multipart/form-data'.

In my Json i want to return a value showing whether successful and a filename.

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public TyoeOfSomeObject UploadFile()
{
    // Get the file from the context and do something with it
    HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files[0];

    // Return either a string or an object to serialise with the required values
    return SomeObject;
}
Jonnio
  • 267
  • 1
  • 3
  • 13

4 Answers4

1

I was having the same issue, and resolved it by setting the response's ContentType and calling the response's Write() function:

C#

String j = jsonParser.AsJson(obj);
Context.Response.ContentType = "application/json; charset=utf-8";
Context.Response.Write(j);

VB

Dim j As String = jsonParser.AsJson(obj)
Context.Response.ContentType = "application/json; charset=utf-8"
Context.Response.Write(j)
Greg
  • 3,086
  • 3
  • 26
  • 39
0

You could set the return type of your function to a string and then use some JSON serializer to serialize your object to JSON and return it as a JSON string. For JSON serialization I use Jayrock I believe ASP .NET has its own JSON libraries now as well.

Justin
  • 1,428
  • 1
  • 13
  • 29
0

You can declare your web method with byte[] as an output parameter. Then you can set ContentType and return any data you want.

If you use WCF instead of ASMX web service you can return Stream or Message in such cases (see Returning raw json (string) in wcf. You can try also return Stream instead of byte[] in the web service if your file is very large. Probably it will also works with ASMX web service.

ASP.Net Webservice Serialization

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • WCF isnt an option. Why would setting an output parameter as Byte[]. Do you mean input parameter? – Jonnio Aug 10 '10 at 16:53
  • Specifying an input param of byte[] gives the folloeing error: Request format is invalid: multipart/form-data. – Jonnio Aug 10 '10 at 16:59
  • 1
    Look at this one http://www.zdnetasia.com/create-a-simple-file-transfer-web-service-with-net-39251815.htm or http://articles.techrepublic.com.com/5100-10878_11-5805105.html. Is it what you are looking for? – Oleg Aug 10 '10 at 19:28
  • No...Unfortunately I dont have a byte[] as i am sending to server with ajax. So i can get the file from the current httpContext no problem. What i cannot do is return json. I think its a limitation of this way of working. – Jonnio Aug 11 '10 at 09:02
  • 1
    I could not find an easy way to set content-type of the respond. Only a custom `IHttpModule`seems me currently a possible solution. So if you use ajax, I'll recommend you one more time to implement `UploadFile` function in **WFC** instead of ASMX web service. You can mix on the same web site ASMX, WFC, MVC etc pages. The code in WCF is not longer and not complexer as ASMX code. Is it so important of have URL like "MyService.asmx/UploadFile" instead of "MyService.svc/UploadFile"? – Oleg Aug 11 '10 at 09:31
  • The example you suggest that uses WCF recomends I send to a web page which then converts the file to a byte[] to make the call to the WCF. I have tried doing something similar by just posting to the page and saving. I still struggle with the response type as it pops open the 'open file' dialog on the browser even if i force the content type to 'application/json' but thanks for your suggestion. – Jonnio Aug 11 '10 at 10:14
  • 1
    You should not make call to the WCF from ASMX web service, but directly use url with WCF web service like "MyService.svc/UploadFile". Then if WCF method returns `Stream` it can change ContentType to any what you want and return a Stream of data. If you already has data as a file you don't need to read the contain in memory, just open file in FileStream and return FileStream, set `transferMode="Streamed"` and `maxReceivedMessageSize` (see http://msdn.microsoft.com/en-us/library/ms789010.aspx). If you will can problems with the implementation, post the code example and I'll try to help you. – Oleg Aug 11 '10 at 10:38
  • I think the issue is something to do with the configuration on my machine. I have set up a simple web form and posted the file to aspx page (not the webservice) but still it tries to open a file. Maybe be its an issue with firefox or the configuration of my IIS of project. I'm nto really looking for a way of making the thing work, I'm looking for the cause of the problem and an understanding of why this is happening. – Jonnio Aug 11 '10 at 11:06
  • I've tested this in IE now and I get the expected behaviour. I think there is something wrong with my installation of FF. Thanks for your help. And yes it is good to see all of the different way in which it is possible to upload a file these days. – Jonnio Aug 11 '10 at 11:11
  • I am glad to hear this! Congratulation! – Oleg Aug 11 '10 at 12:18
0

I was unable to find a way to return a response in json. I don't think its possible without tinkering with the inner workings. The solution I used was to create an aspx that could handle the file, you could ofcourse use .ashx or WCF as described by OLEG.

Jonnio
  • 267
  • 1
  • 3
  • 13