views:

63

answers:

3

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;
}
A: 

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
This doesnt work...Response type will still be text/xml
Jonnio
Sorry, set the `Context.Response.ContentType` to whatever you need =)
Justin
Response type is still text/xml
Jonnio
A: 

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 http://stackoverflow.com/questions/3078397/returning-raw-json-string-in-wcf/3079326#3079326. 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.

http://stackoverflow.com/questions/3156279/asp-net-webservice-serialization/3156369#3156369

Oleg
WCF isnt an option. Why would setting an output parameter as Byte[]. Do you mean input parameter?
Jonnio
Specifying an input param of byte[] gives the folloeing error: Request format is invalid: multipart/form-data.
Jonnio
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
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
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
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
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
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
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
Installed an earlier version of firefox and it works. Praise the lord!!!!
Jonnio
I am glad to hear this! Congratulation!
Oleg
A: 

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