我只是将基本的 UI 示例集成到一个应用程序中。我想要实现的是为一些无法在预览中呈现的文件添加特定的缩略图。比如我会为word、excel、ppt、pdf添加一些图片。我想得到一个固定的图像。
我知道如何生成这些文件的预览,但首先我想尝试一些简单的事情。
我正在检查这个问题Blueimp Jquery 文件上传:不显示缩略图预览图像
在我的例子中,控件可以正确预览视频和图像。
他们谈论在 jquery.fileupload-ui.js 中修改这个函数
_renderPreviews: function (data) {
data.context.find('.preview').each(function (index, elm) {
$(elm).append(data.files[index].preview);
});
},
那是我必须修改才能使这项工作的唯一部分?
如果有人能给我解释一下,控件的内部流程是如何生成图像预览的。我真的很感激。
但是,我在完成后上传的项目的第二次渲染中实现了一些自定义。 看起来比以前好多了。为了实现这一点,我修改了
public ActionResult GetThumbnailFile(string folderServerPath,string name, bool thumbnail = false)
{
var file = GetFile(name, folderServerPath);
var contentType = MimeMapping.GetMimeMapping(file.Name);
return thumbnail ? Thumb(file, contentType) : File(file.FullName, contentType);
}
private ActionResult Thumb(FileInfo file, string contentType)
{
if (contentType.StartsWith("image"))
{
try
{
using (var img = Image.FromFile(file.FullName))
{
var thumbHeight = (int)(img.Height * (ThumbSize / (double)img.Width));
using (var thumb = img.GetThumbnailImage(ThumbSize, thumbHeight, () => false, IntPtr.Zero))
{
var ms = new MemoryStream();
thumb.Save(ms, img.RawFormat);
ms.Position = 0;
return File(ms, contentType);
}
}
}
catch (Exception ex)
{
// todo log exception
}
}
else
{
switch (file.Extension.ToUpperInvariant())
{
case ".LOG":
return File("~/Images/general_documents_preview/txt-icon-128.png", contentType);
case ".PDF":
return File("~/Images/general_documents_preview/pdf-icon-128.png", contentType);
case "JPEG":
//icon = "JPEG-File-icon-128.png";
break;
case "JPG":
//icon = "JPEG-File-icon-128.png";
break;
case "WBMP":
//icon = "Illustrator-File-icon128.png";
break;
case "BMP":
//icon = "BMP-File-icon-128.png";
break;
case "TIF":
//icon = "Web-File-icon-128.png";
break;
case "JP2":
//icon = "Vector-File-icon-128.png";
break;
case "JBIG2":
//icon = "Vector-File-icon-128.png";
break;
case "EMF":
//icon = "Photoshop-File-icon-128.png";
break;
case "GIF":
//icon = "GIF-File-icon-128.png";
break;
case "PNG":
//icon = "PNG-File-icon-128.png";
break;
case "TXT":
return File("~/Images/general_documents_preview/txt-icon-128.png", contentType);
case ".DOC":
return File("~/Images/general_documents_preview/doc-icon-128.png", contentType);
//icon = "doc-icon-128.png";
case ".DOCX":
return File("~/Images/general_documents_preview/docx-icon-128.png", contentType);
//icon = "docx-icon-128.png";
case ".PST":
return File("~/Images/general_documents_preview/pst-icon-128.png", contentType);
//icon = "pst-icon-128.png";
case ".PPT":
return File("~/Images/general_documents_preview/ppt-icon-128.png", contentType);
//icon = "ppt-icon-128.png";
case ".PPTX":
return File("~/Images/general_documents_preview/pptx-icon-128.png", contentType);
//icon = "pptx-icon-128.png";
case ".HTML":
return File("~/Images/general_documents_preview/html-icon-128.png", contentType);
//icon = "html-icon-128.png";
case ".RTF":
return File("~/Images/general_documents_preview/rtf-icon-128.png", contentType);
//icon = "rtf-icon-128.png";
case ".DLL":
return File("~/Images/general_documents_preview/dll-icon-128.png", contentType);
//icon = "dll-icon-128.png";
case "EML":
//icon = "eml-icon-128.png";
break;
case ".FLA":
return File("~/Images/general_documents_preview/fla-icon-128.png", contentType);
case ".SWF":
return File("~/Images/general_documents_preview/fla-icon-128.png", contentType);
//icon = "fla-icon-128.png";
case ".MP3":
return File("~/Images/general_documents_preview/mp3-icon-128.png", contentType);
case ".XLS":
return File("~/Images/general_documents_preview/xls-icon-128.png", contentType);
//icon = "xls-icon-128.png";
case ".XLSX":
return File("~/Images/general_documents_preview/xlsx-icon-128.png", contentType);
//icon = "xlsx-icon-128.png";
default://unknow
return File("~/Images/general_documents_preview/bin-icon-128.png", contentType);
}
}
return Redirect($"https://placehold.it/{ThumbSize}?text={Url.Encode(file.Extension)}");
}
您可以检查下载原始示例项目的更改 https://github.com/ronnieoverby/blue-imp-upload-aspnet-mvc/tree/master/src/jquploadz
这是我在上传文件后添加自定义缩略图的解决方法,假设您使用的是演示示例中的 blueimp javascript 模板 (x-tmpl):https://github.com/blueimp/JavaScript-Templates
<span class="preview">
{% if (file.thumbnailUrl) { %}
<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
{% } else {
// Display relevant icon
var ext = file.name.substr(file.name.lastIndexOf('.') + 1);
var icon = "icons/generic.png";
if (ext === "pdf") {var icon="icons/pdf.png"};
if (ext === "xls" || ext === "xlsx" || ext === "csv" || ext === "ods" || ext === "xlr") {var icon="icons/excel.png"};
if (ext === "psd" || ext === "psb" || ext === "eps" || ext === "tif" || ext === "tiff" || ext === "indd" || ext === "ind" || ext === "bmp" || ext === "ai") {var icon="icons/graphic.png"};
if (ext === "tar" || ext === "rar" || ext === "tgz" || ext === "gz" || ext === "7z" || ext === "sit" || ext === "sitx" || ext === "cab") {var icon="icons/archive.png"};
if (ext === "docx" || ext ==="doc") {var icon="icons/word.png"};
if (ext === "rtf" || ext === "txt" || ext === "log" || ext === "xml" || ext === "css" || ext === "htm" || ext === "html") {var icon="icons/txt.png"};
if (ext === "zip") {var icon="icons/zip.png"};
if (ext === "mov" || ext === "avi" || ext === "m4v" || ext === "3gp" || ext === "mpeg" || ext === "mkv" || ext === "vob" || ext === "aaf" || ext === "asf" || ext === "wmv" || ext === "flv" || ext === "swf" || ext === "mpg" || ext === "mp4") {var icon="icons/mov.png"};
if (ext === "ppt" || ext === "pptx" || ext === "pps") {var icon="icons/ppt.png"};
if (ext === "pkg" || ext === "rpm") {var icon="icons/pkg.png"};
if (ext === "dmg" || ext === "iso" || ext === "bin" || ext === "toast") {var icon="icons/dmg.png"};
if (ext === "key" || ext === "odp") {var icon="icons/keynote.png"};
if (ext === "pages" || ext === "odt") {var icon="icons/pages.png"};
if (ext === "numbers") {var icon="icons/numbers.png"};
if (ext === "wav" || ext === "aif" || ext === "aiff" || ext === "m4a" || ext === "ogg" || ext === "mp3" || ext === "caf" || ext === "aac") {var icon="icons/wav.png"};
if (ext === "fnt" || ext === "fon" || ext === "otf" || ext === "ttf") {var icon="icons/font.png"};
%}<img src="{%=icon%}">{% } %}
</span>
使用当前版本的 jQuery 文件上传器,我在上传之前通过将 80x80 gif (pdf80x80.gif) 放入 img 文件夹并将其添加到 jquery.fileupload-ui.css 的顶部获得了默认的 pdf 预览图标
span.preview{
background: url('../img/pdf80x80.gif') !important;
display: block;
width: 80px;
height: 80px;
}
对于帖子上传预览,在 index.html 中,我在标记的部分中进行了更改 “显示可供下载文件的模板”我改了
<span class="preview">
{% if (file.thumbnailUrl) { %}
<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
{% } %}
</span>
到
<span class="preview">
{% if (file.thumbnailUrl) { %}
<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
{% } else { %}
<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="./img/pdf80x80.gif"></a>
{% } %}
</span>
警告:我的网站只允许上传 jpg 或 pdf,所以我只需要一个非图像默认图标。