M
Milanesi Stefano
Guest
Helo,
I'm developing a windows form application (c#) that calls WEB API services (developed by me) and in particular I'm trying to "downloading" an Excel file (report) that it was previously created in a server folder ("Stampe").
I'm in trouble to "manage" client side, the response (HttpResponseMessage) from WEB API service.
[HttpGet]
public HttpResponseMessage GetFile(string FilePath, string FileName)
{
return GetFileStampa(FilePath, FileName);
}
public HttpResponseMessage GetFileStampa(string FilePath, string fileName)
{
var path = Path.Combine(FilePath, fileName);
var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(path, FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
return response;
}
string FilePath = @"........\Stampe\";
string FileName = @"Test.xlsx";
string UrlGetFile = Url + "/File?" + "FilePath=" + FilePath + "&FileName=" + FileName;
Uri UriGetFile = new Uri(UrlGetFile);
HttpWebRequest request = WebRequest.Create(UriGetFile) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "text/xml";
string results = string.Empty;
HttpWebResponse response;
using (response = request.GetResponse() as HttpWebResponse)
{
Stream objStream = response.GetResponseStream();
BinaryReader breader = new BinaryReader(objStream);
byte[] data = breader.ReadBytes((int)response.ContentLength);
File.WriteAllBytes("d:\\temp\\MyTest.xlsx", data);
}
the instruction "response.ContentLength" is always to -1. Message error: Non-negative number required
Where am I doing wrong (client or server side)?
Best
Stefano
Continue reading...
I'm developing a windows form application (c#) that calls WEB API services (developed by me) and in particular I'm trying to "downloading" an Excel file (report) that it was previously created in a server folder ("Stampe").
I'm in trouble to "manage" client side, the response (HttpResponseMessage) from WEB API service.
- SERVER side:
[HttpGet]
public HttpResponseMessage GetFile(string FilePath, string FileName)
{
return GetFileStampa(FilePath, FileName);
}
public HttpResponseMessage GetFileStampa(string FilePath, string fileName)
{
var path = Path.Combine(FilePath, fileName);
var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(path, FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
return response;
}
- CLIENT side (windows form):
string FilePath = @"........\Stampe\";
string FileName = @"Test.xlsx";
string UrlGetFile = Url + "/File?" + "FilePath=" + FilePath + "&FileName=" + FileName;
Uri UriGetFile = new Uri(UrlGetFile);
HttpWebRequest request = WebRequest.Create(UriGetFile) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "text/xml";
string results = string.Empty;
HttpWebResponse response;
using (response = request.GetResponse() as HttpWebResponse)
{
Stream objStream = response.GetResponseStream();
BinaryReader breader = new BinaryReader(objStream);
byte[] data = breader.ReadBytes((int)response.ContentLength);
File.WriteAllBytes("d:\\temp\\MyTest.xlsx", data);
}
the instruction "response.ContentLength" is always to -1. Message error: Non-negative number required
Where am I doing wrong (client or server side)?
Best
Stefano
Continue reading...