如何上传Pdf文件并让用户看到内容 [英] How do I upload a Pdf file and make users a user see the contents

查看:101
本文介绍了如何上传Pdf文件并让用户看到内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我有一个电子图书馆,我希望用户能够在线阅读这些书籍。我怎么做到的?



这是我的控制器:



[HttpGet]

public ActionResult NewBook()

{

返回查看();

}



[HttpPost]

[ValidateAntiForgeryToken]

公共ActionResult NewBook(图书馆书籍)

{

if(ModelState.IsValid)

{

if(Request.Files.Count> 0)

{

System.Random randomInteger = new System.Random();

int genNumber = randomInteger.Next(1000000);

HttpPostedFileBase file = Request.Files [ 0];

if(file.ContentLength> 0 && file.ContentType.ToUpper()。包含(JPEG)|| file.ContentType.ToUpper()。包含(PNG) || file.ContentType.ToUpper()。包含(JPG ))

{

WebImage img = new WebImage(file.InputStream);

if(img.Width> 500)

{

img.Resize(宽度:400,高度:400,preserveAspectRatio:true,preventEnlarge:true);

}

string fileName = Path.Combine(Server.MapPath(〜/ Uploads /),Path.GetFileName(genNumber + file.FileName));

img.Save( fileName);

book.LibraryFilePhoto = fileName;

}

}

// pdf上传部分

if(Request.Files.Count> 0)

{

HttpPostedFileBase file = Request.Files [0];

if(file.ContentLength> 0 && file.ContentType.ToUpper()。包含(PDF)|| file.ContentType.ToUpper()。包含(PNG)|| file.ContentType.ToUpper( )。包含(JPG))

{

var filename = Path.GetFileName(file.FileName);

var path = Path.Combine(Server.MapPath(〜/ Uploads /),filename);

file.SaveAs(path);

book.LibraryFile = filename;

}

}

db.Library .Add(book);

db.SaveChanges();

TempData [Save] =Book++ book.BookTitle ++已被保存;

返回RedirectToAction(索引);

}



var hold = book ;

返回查看(书);

}

Please i Have an e-Library and i want users to be able to read the books online. How do i get that done??

Here is my Controller:

[HttpGet]
public ActionResult NewBook()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewBook (Library book)
{
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
System.Random randomInteger = new System.Random();
int genNumber = randomInteger.Next(1000000);
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0 && file.ContentType.ToUpper().Contains("JPEG") || file.ContentType.ToUpper().Contains("PNG") || file.ContentType.ToUpper().Contains("JPG"))
{
WebImage img = new WebImage(file.InputStream);
if (img.Width > 500)
{
img.Resize(width: 400, height: 400, preserveAspectRatio: true, preventEnlarge: true);
}
string fileName = Path.Combine(Server.MapPath("~/Uploads/"), Path.GetFileName(genNumber + file.FileName));
img.Save(fileName);
book.LibraryFilePhoto = fileName;
}
}
//section for pdf upload
if (Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0 && file.ContentType.ToUpper().Contains("PDF") || file.ContentType.ToUpper().Contains("PNG") || file.ContentType.ToUpper().Contains("JPG"))
{
var filename = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Uploads/"), filename);
file.SaveAs(path);
book.LibraryFile = filename;
}
}
db.Library.Add(book);
db.SaveChanges();
TempData["Save"] = "Book" + " " + book.BookTitle + " " + "has Been Saved";
return RedirectToAction("Index");
}

var hold = book;
return View(book);
}

推荐答案

对不起,我不知道你是什么试着做你的代码。当我看到 System.Random 时,我的耐心已经结束。怎么可能与你的问题有关。



好​​吧。在文件上传时,我希望它足以说:1)你上传的文件类型没有区别;这完全无关紧要; 2)有大量的代码示例显示如何使用不同的技术在不同级别上进行文件上传。



现在,主要的事情是:你永远不应该依赖任何软件显示PDF。 PDF不是W3标准的一部分。即使PDF经常在浏览器中显示,也不能保证您的用户具有此功能。这通常作为浏览器插件实现。并非所有用户都拥有此选项,并非所有用户都会使用此选项。此外,您不应该假设所有用户都拥有任何PDF软件。但这根本不是问题。



您只需要将PDF放在锚标记中,作为 href中引用的文件属性。在这种情况下,具有PDF插件的用户将其加载到浏览器中。在所有其他情况下,用户可以选择使用PDF的默认软件产品集打开文件(如果有),或者下载文件。如果没有任何PDF软件,用户将只能选择一个:下载文件。这是相当公平的,因为用户将始终能够稍后查看该文档。



你不需要做任何特别的事情,事实上,没有必要做任何事情。



一种替代方法是根本不提供PDF,而是将PDF内容表示为HTML。我会高度劝阻这项活动:不仅是劳动力,而且这种表现的质量总是值得怀疑。 PDF具有非常不同的渲染概念;几乎与HTML相反; PDF是一种布局固定的电子纸;如果可以的话,我宁愿永远不要在屏幕上阅读PDF,也不要在打印前计算屏幕预览或进行健全性检查。 PDF对其他任何东西都不好;填写PDF表格这样的事情只不过是折磨用户。如果您真的需要PDF支持,请将其保持在最低限度。



-SA
Sorry, I have no idea what you are trying to do in your code. My patience has ended when I saw System.Random. How can it possibly be related to your question.

All right. On file uploading, I hope it will be enough to say: 1) it makes no difference what type of file you uploading; it's totally irrelevant; 2) there is enormous number of code samples showing how to do file upload on different levels using different technologies.

Now, main thing: you should never rely on any software which shows PDF. PDF is not a part of W3 standards. Even though PDFs are routinely shown in browsers, nothing guarantees that your user has this feature. This is usually implemented as a browser plug-ins. Not all of your users will have this option, and not all will use it. Moreover, you should not assume that all user have any PDF software at all. But this is not a problem at all.

You just need to put your PDF in a anchor tag, as a file referenced in the href attribute. In this case, the user having a PDF plug-in will have it loaded in the browser. In all other cases, the user will have either a choice to open the file using default software product set for PDFs, if any, or download the file. If there is not any PDF software, the user will be give only one choice: to download a file. This is quite fair, because the user will always be able to view the document later.

There is nothing special you need to do, and, in fact, there is no any need to do anything else.

One alternative way would be not presenting PDF at all, but, instead, representing PDF content as HTML. I would highly discourage this activity: not only it's labor-taking, but also the quality of such presentation will always be questionable. PDF has very different concept of rendering; pretty much opposite to HTML; PDF is a kind of electronic paper with fixed layout; if I could, I would prefer to never read PDF on screen, not counting screen preview before printing or for sanity check. PDF is hardly good for anything else; and such thing as filling PDF forms is nothing but torturing the users. If you really bound to PDF support, keep it to minimum.

—SA


这篇关于如何上传Pdf文件并让用户看到内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆