Загрузка архивированной версии папкиC#

Место общения программистов C#
Ответить
Anonymous
 Загрузка архивированной версии папки

Сообщение Anonymous »

Я пытаюсь загрузить заархивированную версию папки с именем XSLT. Это код, который я написал, однако загруженный файл xslt.zip невозможно разархивировать. Когда я использую стороннее приложение (7zip), оно говорит, что в заголовках есть ошибка. Каково обходное решение этой проблемы?

Код: Выделить всё

[HttpGet]
[JWTAuthentication]
public HttpResponseMessage DownloadFiles()
{
logger.LogTraceStart();

string folderPath = HttpContext.Current.Server.MapPath("~/XSLT");
string zipPath = HttpContext.Current.Server.MapPath("~/XSLTlol.zip");

if (File.Exists(zipPath))
{
File.Delete(zipPath);
}

try
{
// Create the ZIP file
if (Directory.Exists(folderPath))
{
// Ensure the directory and files are accessible and not locked
ZipFile.CreateFromDirectory(folderPath, zipPath, CompressionLevel.Optimal, true);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Directory not found.");
}

// Verify the ZIP file was created
if (!File.Exists(zipPath))
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to create ZIP file.");
}

// Create the HTTP response
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(zipPath, FileMode.Open, FileAccess.Read));

// result.Content.AddHea

//result.AddHeader("content-disposition", "attachment; filename=" + "_Templates.Zip");

result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "XSLTlol"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

return result;
}
catch (Exception ex)
{
logger.LogError(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "An error occurred while creating the ZIP file.");
}
finally
{

this.session.Flush();
logger.LogTraceEnd();
}

}
Идеальным результатом должен быть загружаемый zip-файл, который можно распаковать и просмотреть при использовании конечной точки API HttpGet.

Подробнее здесь: https://stackoverflow.com/questions/787 ... f-a-folder
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»