Meine WCF-Dienstmethode in C#:
Code: Select all
[OperationContract()]
public Stream GetCsvFile(int id)
{
string s = ...;
WebOperationContext.Current.OutgoingResponse.ContentType = "text/csv";
WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = "attachment; filename=\"file1.csv\"";
return GenerateStreamFromString(s);
}
public Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
Code: Select all
$.ajax({
type: "POST",
url: serviceUrl,
cache: false,
data: data,
dataType: "text",
contentType: "application/json; charset=utf-8",
success: function() {
// TODO...
}
});
In einer alten VB.NET-Version derselben Anwendung funktionierte Folgendes in einem .aspx-Seiten-Code-Behind:
Code: Select all
Response.Clear()
Response.ContentType = "text/csv"
Response.AddHeader("content-disposition", "attachment; filename="file1.csv")
Response.Write(s)
Response.End()
Wie kommt es also, dass es nicht funktioniert, wenn ich versuche, einen WCF-Dienst mit jQuery aufzurufen?
Mobile version