gpt4 book ai didi

Downloading file from database with ajax (File is corrupted or cannot be opened)(使用AJAX从数据库下载文件(文件已损坏或无法打开))

转载 作者:bug小助手 更新时间:2023-10-27 20:58:13 25 4
gpt4 key购买 nike



I am saving a file to the database with the codes below.

我正在用下面的代码将文件保存到数据库中。


controller.cs,

pk10.cs,


[HttpPost]
public IActionResult AddFile(IFormFile file, TaskListAttachment taskListAttachment)
{
if (file != null && file.Length > 0)
{
var fileName = Path.Combine("wwwroot/files", Path.GetFileName(file.FileName));
using (var stream = new FileStream(fileName, FileMode.Create))
{
file.CopyTo(stream);
}
taskListAttachment.FileName = file.FileName;
taskListAttachment.ContentType = file.ContentType;
taskListAttachment.Data = new byte[file.Length];
taskListAttachment.AddDateTime = DateTime.Now;
taskListAttachment.FilePath = fileName;
taskListAttachment.TaskId = Convert.ToInt32(Request.Form.Where(x => x.Key == "TaskId").FirstOrDefault().Value);
taskListAttachment.AddUserId = Convert.ToInt32(Request.Form.Where(x => x.Key == "AddUserId").FirstOrDefault().Value);
taskListAttachmentManager.itemAdd(taskListAttachment);
var addValue = JsonConvert.SerializeObject(taskListAttachment);
return Json(addValue);
}
else
{
return Json(null);
}
}

cshtml,

Cshtml,


<script>
$("#fileInput").change(function () {
var fileName = $(this).val();
var fileExtension = fileName.split('.').pop().toLowerCase();
$("#fileExtension").val(fileExtension);
});

$("#uploadButton").click(function () {
var addUserId = $("#addUserId").data("user-id");
var taskId = $("#taskId").data("task-id");
var fileExtension = $("#fileExtension").val();
var formData = new FormData();
formData.append("AddUserId", addUserId);
formData.append("TaskId", taskId);
formData.append("FileExtension", fileExtension);
formData.append("file", $("#fileInput")[0].files[0]);

$.ajax({
url: "/TaskList/AddFile/",
type: "Post",
data: formData,
contentType: false,
processData: false,
success: function (result) {
var dismissModal = document.getElementById("dismissButton").click();
attachmentalert();
var FileInput = $('#fileInput');
FileInput.val('').focus();
}
});
});
</script>

I then wrote the following codes to download the saved file,

然后我编写了以下代码来下载保存的文件,


controller.cs,

Controler.cs


public IActionResult DownloadFile(int id)
{
var attachment = taskListAttachmentManager.itemGetById(id);
if (attachment != null)
{
return File(attachment.Data, attachment.ContentType, attachment.FileName);
}
else
{
return NotFound();
}
}

cshtml,

Cshtml,


<a href="@Url.Action("DownloadFile", "TaskList", new {id = item.AttachmentID})" class="text-dark fs-4 link lh-sm">@item.FileName</a>

But when I download files, they download incorrectly or corrupted. If I download jpeg or png, the image won't open, if I download rar or zip, I get a warning that the archive is corrupt or incorrect. The same goes for office files and pdf.

但当我下载文件时,它们下载不正确或已损坏。如果我下载jpeg或png,图像将无法打开,如果我下载rar或压缩包,我会收到存档已损坏或不正确的警告。Office文件和pdf也是如此。


What is the problem?

有什么问题吗?


Thanks.

谢谢.


更多回答

I checked it on a txt file and when I downloaded it, it came up empty.

我在一个txt文件上检查了一下,当我下载它的时候,它是空的。

The site cannot be reached, is the address correct?

无法访问该站点,地址正确吗?

I noticed that you shared the code snippets both for uploading and downloading, could you pls help confirm if the files uploaded by your code is good to open directly? I mean, could you pls open the png/jpeg/zip/txt file stored in wwwroot/files directly? If yes, then the issue might relate to the download, if not, the issue might relate to the upload.

我注意到你分享了上传和下载的代码片段,你能帮忙确认一下你的代码上传的文件是否可以直接打开吗?我的意思是,你能直接打开存储在wwwroot/files中的png/jpeg/压缩/txt文件吗?如果是,则问题可能与下载有关,如果不是,问题可能与上载有关。

this doesn't look right: taskListAttachment.Data = new byte[file.Length]; Seems like that should be set to the stream. (you may want stream.Position = 0; first, btw...) If this is untrusted user input, you should be aware that the code, as is, is vulnerable to attack.

这看起来不太对劲:taskListAttachment.Data=new byte[file.Length];似乎应该将其设置为流。(您可能需要Stream.Position=0;首先,btw...)如果这是不受信任的用户输入,您应该意识到代码按原样很容易受到攻击。

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com