asp.net-mvc - 如何在 ASP.Net MVC 中使用 DropZone 显示 PDF 并使其可下载
问题描述
我在我的 ASP.Net MVC 项目中使用 DropZone 进行文件上传。
一切都很好,可以使用以下代码上传图像/PFD格式文件
这是我的代码
1)View和jQuery上传图片
@if (!Model.GalleryImages.Any())
{
<h3>There are no gallery images for this product.</h3>
}
<form action="/home/SaveGalleryImages" method="post" enctype="multipart/form-data" class="dropzone" id="dropzoneForm">
@Html.HiddenFor(model => model.Id)
<div class="fallback">
<input type="file" name="file" multiple />
<input type="submit" value="Upload" />
</div>
</form>
<br /><br />
@foreach (var image in Model.GalleryImages)
{
<div style="display: inline-block;margin-right: 15px;" class="dropzone">
<img src="/Images/Uploads/Employee/@Model.Id/Gallery/Thumbs/@image" />
@Html.ActionLink("delete", "DeleteImage", "Shop", new { @class = "deleteimage", data_name = image })
</div>
}
<link href="~/Scripts/dropzone/basic.css" rel="stylesheet" />
<link href="~/Scripts/dropzone/dropzone.css" rel="stylesheet" />
@section Scripts{
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/dropzone/dropzone.js"></script>
<script type="text/javascript">
$(document).ready(function () {
Dropzone.options.dropzoneForm = {
acceptedFiles: "image/*",
init: function() {
this.on("complete", function (file) {
if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
location.reload();
}
});
this.on("sending", function (file, xhr, formData) {
formData.append("id", @Model.Id);
});
}
};
//////////////////////////////////////////////////////////////
});
</script>
这是动作方法
[HttpPost]
public void SaveGalleryImages(User model)
{
int id = model.Id;
// Loop through files
foreach (string fileName in Request.Files)
{
// Init the file
HttpPostedFileBase file = Request.Files[fileName];
var supportedTypes = new[] { "pdf" };
var fileExt = System.IO.Path.GetExtension(file.FileName).Substring(1);
// Check it's not null
if (file != null && supportedTypes.Contains(fileExt))
{
// Set directory paths
var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));
string pathString1 = Path.Combine(originalDirectory.ToString(), "Employee\\" + id.ToString() + "\\Gallery");
// Set image paths
var path = string.Format("{0}\\{1}", pathString1, file.FileName);
// Save original and thumb
file.SaveAs(path);
}
else
{
// Set directory paths
var originalDirectory = new DirectoryInfo(string.Format("{0}Images\\Uploads", Server.MapPath(@"\")));
string pathString1 = Path.Combine(originalDirectory.ToString(), "Employee\\" + id.ToString() + "\\Gallery");
string pathString2 = Path.Combine(originalDirectory.ToString(), "Employee\\" + id.ToString() + "\\Gallery\\Thumbs");
// Set image paths
var path = string.Format("{0}\\{1}", pathString1, file.FileName);
var path2 = string.Format("{0}\\{1}", pathString2, file.FileName);
// Save original and thumb
file.SaveAs(path);
WebImage img = new WebImage(file.InputStream);
img.Resize(200, 200);
img.Save(path2);
}
}
}
所以,现在有了上面的代码,我就可以成功上传图片和 PDF 文件了。但是,我希望能够在视图中查看 PDF 文件。
我上面的查看代码只能显示图像,效果很好。但是,我怎样才能显示上传到 Gallery 文件夹中的 PDF?
请建议。
非常感谢您的帮助。
解决方案
推荐阅读
- typescript - 从仅定义一个选项的多个选项中为变量赋值
- c++ - /usr/include/stdlib.h:133:35:错误:在标记“(”之前缺少二元运算符
- javascript - Three.js 使用 Raycaster 检测 ArrowHelper 的线和锥子项
- snowflake-cloud-data-platform - 自动增量增长速度快于行数?
- asp.net-core - 如何处理 ASP.NET Core 操作中的未知参数
- python - 安装 http-request-randomizer 会出现安装错误
- python - Python-Requests 验证参数如何工作?SSL 证书
- azure - 我应该为静态网站的 Azure Pipeline 的 rootDirectory 指定什么?
- ffmpeg - FFMPEG 程序来裁剪图像并并排合并它们
- groovy - JMETER - WebDriver 采样器 - Groovy - 动态名称