<input type="file" name="WenJian" id="FileUpload1" style="display:none;" />
<button type="button" id="UploadFile1" οnclick="UploadFile()" class="btn btn-primary">保存</button>
function UploadFile() {
var currentFile = document.getElementById("FileUpload1").files;
var file = currentFile[0];
var formData = new FormData();
formData.append("MyFile", file)
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
//信息填写
UserpapersAdd(xhr.responseText)
}
}
xhr.open("post", "/Signup/AjaxSaveAsID", true)
xhr.send(formData);
}
[HttpPost]
public string AjaxSaveAsID(HttpPostedFileBase MyFile)
{
try
{
//得到的名字是文件在本地机器的绝对路径
var strLocalFullPathName = oS_User.certificate + MyFile.FileName;
//提取出单独的文件名,不需要路径
var strFileName = Path.GetFileName(strLocalFullPathName);
string SJ = DateTime.Now.ToFileTime().ToString();
//定义服务器的文件夹,用来保存文件
var strServerFilePath = Server.MapPath("/docs/");
//将接收到文件保存在服务器指定上当
MyFile.SaveAs(Path.Combine(strServerFilePath, SJ + strFileName));
return (SJ + strFileName);
}
catch (Exception)
{
return "Error";
}
}