i want to write a api for download all file in zipped , file path will come from database
Loading
i want to write a api for download all file in zipped , file path will come from database
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandeep KumarPosted Jul 19, 2023, 11:43 AM
still error showing
Amit MohantyPosted Jul 19, 2023, 11:38 AM
Use client.GetByteArrayAsync(fileUrl)
Sandeep KumarPosted Jul 19, 2023, 11:18 AM
Error showing
Amit MohantyPosted Jul 19, 2023, 11:14 AM
Change the above code with below code and try again:
Sandeep KumarPosted Jul 19, 2023, 11:08 AM
Please i dont't want in mvc
I want Normal aso.net web api
Pls go through my code
[Authorize]
[HttpPost]
public HttpResponseMessage DownloadImage(JObject jsonParam)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
try
{
UserIdentity UI = new UserIdentity((ClaimsIdentity)User.Identity);
var RoleId = Convert.ToInt32(UI.RoleId);
var UserID = Convert.ToInt32(UI.UserID);
PmsInsertUpdateData _KD = new PmsInsertUpdateData();
// var CD_Dtls = _KD.GetPmsUploadedImage(jsonParam);
DataTable DtImage = _KD.DownloadImage(jsonParam);
string filePath = string.Empty;
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (DataRow row in DtImage.Rows)
{
filePath = row["FileName"].ToString();
string localPath = new Uri(filePath).LocalPath;
zip.AddFile(filePath, "Files");
}
//Set the Name of Zip File.
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Zip File to MemoryStream.
zip.Save(memoryStream);
//Set the Response Content.
response.Content = new ByteArrayContent(memoryStream.ToArray());
//Set the Response Content Length.
response.Content.Headers.ContentLength = memoryStream.ToArray().LongLength;
//Set the Content Disposition Header Value and FileName.
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = zipName;
//Set the File Content Type.
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
return response;
}
}
}
catch (Exception ex)
{
response = Request.CreateResponse(JsonConvert.DeserializeObject("{\"StatusCode\":\"500\",\"ErrorMessage\":\"" + ex.Message + "\"}"));
return response;
}
// = Request.CreateResponse(HttpStatusCode.OK, response);
}
Sandeep KumarPosted Jul 19, 2023, 10:56 AM
Error :showing
{
"StatusCode": "500",
"ErrorMessage": "URI formats are not supported."
}
Actually I want to retreive file from live url
l like :-http://148.72.207.115:4458/BBGS_Unit3_BD_GEN_2021-2022_sample_001.png
Pls Go through my code .i don't want in mvc ,i want normal web api
Amit MohantyPosted Jul 19, 2023, 10:38 AM
Check this
Sandeep KumarPosted Jul 19, 2023, 10:24 AM
I don;t need in mvc
It is working for local path but i want to fetch file from Url
My Url Is like :-http://148.72.207.115:4458/BBGS_Unit3_BD_GEN_2021-2022_sample_001.png
My Code is like
[Authorize]
[HttpGet]
public HttpResponseMessage DownloadImage(Int Id)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
try
{
UserIdentity UI = new UserIdentity((ClaimsIdentity)User.Identity);
var RoleId = Convert.ToInt32(UI.RoleId);
var UserID = Convert.ToInt32(UI.UserID);
PmsInsertUpdateData _KD = new PmsInsertUpdateData();
// var CD_Dtls = _KD.GetPmsUploadedImage(Id);
DataTable DtImage = _KD.DownloadImage(jsonParam);
string filePath = string.Empty;
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (DataRow row in DtImage.Rows)
{
filePath = row["FileName"].ToString();
string localPath = new Uri(filePath).LocalPath;
zip.AddFile(filePath, "Files");
}
//Set the Name of Zip File.
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Zip File to MemoryStream.
zip.Save(memoryStream);
//Set the Response Content.
response.Content = new ByteArrayContent(memoryStream.ToArray());
//Set the Response Content Length.
response.Content.Headers.ContentLength = memoryStream.ToArray().LongLength;
//Set the Content Disposition Header Value and FileName.
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = zipName;
//Set the File Content Type.
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
return response;
}
}
}
catch (Exception ex)
{
response = Request.CreateResponse(JsonConvert.DeserializeObject("{\"StatusCode\":\"500\",\"ErrorMessage\":\"" + ex.Message + "\"}"));
return response;
}
// = Request.CreateResponse(HttpStatusCode.OK, response);
}
Amit MohantyPosted Jul 19, 2023, 9:09 AM
Try this
Sandeep KumarPosted Jul 19, 2023, 7:24 AM
it is working https://www.aspsnippets.com/Articles/Download-Compressed-Zip-File-from-Web-API-in-ASPNet-MVC.aspx
but when i want to download from url it is not working pls guide
how can i download multiple file from url in Zip File
My Url Is like :-http://148.72.207.115:4458/BBGS_Unit3_BD_GEN_2021-2022_sample_001.png
Vishal YelvePosted Jul 18, 2023, 6:30 AM
Hi Sandeep,
Do refer below links
https://www.aspsnippets.com/Articles/Download-Compressed-Zip-File-from-Web-API-in-ASPNet-MVC.aspx
https://stackoverflow.com/questions/41383338/how-to-download-a-zipfile-from-a-dotnet-core-webapi