使用 jquery mvc 4 上传 pdf 文件
uploading pdf file using jquery mvc 4
我正在尝试使用 jquery 上传一个文件,它可以很好地处理文本文件,但是当我尝试上传一个 pdf 文件时,它会出错并且总是 return 0 到 Request.Files.Count.
实际上我想在上传之前预览文档,所以首先我在预览文件夹中上传一个文档,然后我使用 pdf 查看器在 div 中显示它,如果他 select 另一个文档我将删除最后一个文档并上传新文档,但我的问题是我的代码只适用于 .txt 文件,我想要 pdf
我试了2个小时,不明白错误,请帮忙!
客户端代码
HTML
<input id="file" class="form-control-file" type="file" name="file" placeholder="Document Upload" />
Jquery
$("#file").change(function () {
var formData = new FormData();
var totalFiles = document.getElementById("file").files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("file").files[i];
formData.append("file", file);
}
$.ajax({
type: "POST",
url: '/Admin/PreviewUpload',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
alert('succes!!');
},
error: function (error) {
alert("errror");
}
});
});
服务器端代码
[HttpPost]
public ActionResult PreviewUpload()
{
if (Request.Files.Count > 0)
{
foreach (string files in Request.Files)
{
var _file = Request.Files[files];
FileInfo Fi = new FileInfo(Path.GetFileName(_file.FileName));
string fileExtention = Fi.Extension;
if (_file != null)
{
if (fileExtention == ".PDF")
{
string fileName = Path.GetFileName(_file.FileName);
if (_file.ContentLength <= 120000000)
{
_file.SaveAs(Server.MapPath("~/PreviewPDF/" + fileName));
}
string path = "/PreviewPDF/" + Path.GetFileName(_file.FileName);
ViewData["error"] = path;
return Json(new
{
Success = path
});
}
else
{
return Json(new
{
fileError = "Only Support PDF"
});
}
}
else
{
return Json(new
{
error = "Please Select the file"
});
}
}
}
return Json(new
{
error = "Please Select the file"
});
}
如果要上传大文件需要更改配置。
有关详细信息,请单击此处 Click me
You need to add/update the values of "executionTimeout",
"maxRequestLength" & "maxAllowedContentLength" properties if not
already added in the "Web.config" file, as shown below.
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.2" /> // here your project version
<!-- executionTimeout = 30hrs (the value is in seconds) and maxRequestLength = 1GB (the value is in Bytes) -->
<httpRuntime targetFramework="4.5.2" executionTimeout="108000" maxRequestLength="1073741824" />
</system.web>
<system.webServer>
<!-- maxAllowedContentLength = 1GB (the value is in Bytes) -->
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
我正在尝试使用 jquery 上传一个文件,它可以很好地处理文本文件,但是当我尝试上传一个 pdf 文件时,它会出错并且总是 return 0 到 Request.Files.Count.
实际上我想在上传之前预览文档,所以首先我在预览文件夹中上传一个文档,然后我使用 pdf 查看器在 div 中显示它,如果他 select 另一个文档我将删除最后一个文档并上传新文档,但我的问题是我的代码只适用于 .txt 文件,我想要 pdf
我试了2个小时,不明白错误,请帮忙!
客户端代码
HTML
<input id="file" class="form-control-file" type="file" name="file" placeholder="Document Upload" />
Jquery
$("#file").change(function () {
var formData = new FormData();
var totalFiles = document.getElementById("file").files.length;
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("file").files[i];
formData.append("file", file);
}
$.ajax({
type: "POST",
url: '/Admin/PreviewUpload',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
alert('succes!!');
},
error: function (error) {
alert("errror");
}
});
});
服务器端代码
[HttpPost]
public ActionResult PreviewUpload()
{
if (Request.Files.Count > 0)
{
foreach (string files in Request.Files)
{
var _file = Request.Files[files];
FileInfo Fi = new FileInfo(Path.GetFileName(_file.FileName));
string fileExtention = Fi.Extension;
if (_file != null)
{
if (fileExtention == ".PDF")
{
string fileName = Path.GetFileName(_file.FileName);
if (_file.ContentLength <= 120000000)
{
_file.SaveAs(Server.MapPath("~/PreviewPDF/" + fileName));
}
string path = "/PreviewPDF/" + Path.GetFileName(_file.FileName);
ViewData["error"] = path;
return Json(new
{
Success = path
});
}
else
{
return Json(new
{
fileError = "Only Support PDF"
});
}
}
else
{
return Json(new
{
error = "Please Select the file"
});
}
}
}
return Json(new
{
error = "Please Select the file"
});
}
如果要上传大文件需要更改配置。
有关详细信息,请单击此处 Click me
You need to add/update the values of "executionTimeout", "maxRequestLength" & "maxAllowedContentLength" properties if not already added in the "Web.config" file, as shown below.
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.2" /> // here your project version
<!-- executionTimeout = 30hrs (the value is in seconds) and maxRequestLength = 1GB (the value is in Bytes) -->
<httpRuntime targetFramework="4.5.2" executionTimeout="108000" maxRequestLength="1073741824" />
</system.web>
<system.webServer>
<!-- maxAllowedContentLength = 1GB (the value is in Bytes) -->
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>