关于上传pdf 俩个路径 :1 保存路径(文件存放处 物理路径) 2:访问路径(对应物理路径的虚拟路径)
在配置文件中设置:
<add key="UploadPath" value="E://Upload" />
<add key="UploadUrl" value="http://192.168.103.5:80/" />
可以看到保存文件到 本计算机的 E盘下upload文件夹
而访问是根据 ip地址加端口号 这个是 iis 设置一个端口来对应E盘的这个upload文件夹
设置好 俩个路径之后 开始上传的按钮 程序功能实现
public JsonResult UploadPdf(HttpPostedFileBase file, string uploadUrl)
{
String Result = "false";
var FileContentType = file.ContentType;
String serverMapPath = ConfigurationManager.AppSettings["UploadPath"];
//这里可以设置 文件类型 以及大小
if (FileContentType == "application/pdf" && file.ContentLength <= 3 *1024*1024)
{
//这里设置文件夹名称
if(!Directory.Exists(serverMapPath+@"\text"))
{
Directory.CreateDirectory(serverMapPath+@"\text"); // 创建文件夹
}
//路径全拼操作 将文件名的完整路径拼好
var fileName = System.IO.Path.GetFileName(file.FileName);
var filePhysicalPath = Path.Combine(serverMapPath + @"\text\", fileName);
var url = serverMapPath + @"\text\" + fileName;
//查看是否存在 存在就删除不存在 就保存
if (System.IO.File.Exists(url))
{
System.IO.File.Delete(filePhysicalPath); /// 若存在 则删除
}
file.SaveAs(filePhysicalPath); /// 存储
return Json(new { uploadUrl = uploadUrl, fileName = fileName });
}
return Json(new { uploadUrl = uploadUrl, fileName = "false" });
}
页面 控件 以及 js
<div class="form-group">
<label class="col-md-3 control-label">
Attachments<br/>
(The size of the pdf file can not exceed 3MB)
</label>
<div class="col-md-4">
<form id="uploadForm" enctype="multipart/form-data">
<input id="file" type="file" name="file" class="input-inline" style="display:inline" />
<input type="button" class="btn red inline" value=" Upload " id="UploadCountryPdf" onclick="UploadPdf()" />
</form>
</div>
</div>
function UploadPdf() {
if ($("#file").val() == "" || $("#file").val() == undefined) {
ShowMsgBox("Please select the file", "false");
return;
}
$.ajax({
url: '/RitzTours/Basic/UploadPdf',
type: 'POST',
cache: false,
data: new FormData($('#uploadForm')[0]),
processData: false,
contentType: false
}).done(function (res) {
if (res != "false") {
//显示 出来上传的pdf 名字和路径
$("#PdfName").text(res);
$("#PDFUrl").attr("href", $("#hiddUrl").val() + "/text/" + res);
$("#PDFUrl").attr("target", "_blank");
ShowMsgBox("Upload Successfully!", "false");
} else {
ShowMsgBox("The size of the pdf file can not exceed 3MB!", "false");
}
}).fail(function (res) {
ShowMsgBox("Upload failed!", "false");
})
}