標籤:scala keyword save filename turn cat win 資料 exists
html前台
<form action="/api/Manuscript/AddReviewOpinionFile" method="post" enctype="multipart/form-data"> //這個方法僅限於測試使用
選擇圖片: <input type="file" name="uploads" multiple > //multiple 這個屬性是在html5中新增一個附件文字框可上傳多個附件
<input type="submit">
</form>
註: 在實際開發中應該是非同步form表單提交,而非同步
非同步上傳:
//方式1$(function ()
{ $(‘#form2‘).ajaxForm
({ success: function (responseText) { alert(responseText); } });
});
//這種方式是點擊完windows檔案框後執行ajax操作
//方式2(通過ajaxSubmit直接執行ajax操作)
$(function ()
{ $(".but2").click(function ()
{ $(‘#form2‘).ajaxSubmit({ success: function (responseText) { alert(responseText); } });
}); });
//這種方式是點擊提交按鈕執行ajax
註: 上述倆中方式需要匯入jquery.js和jquery.form.js 倆個jquery包
//方式3
<input id="fileinfo" type="file" class="notFormFile" /><button type="button" class="btnNotForm">上傳4</button>
//方式4$(".btnNotForm").click(function () { var formData = new FormData();//初始化一個FormData對象 formData.append("files", $(".notFormFile")[0].files[0]);//將檔案塞入FormData $.ajax({ url: "/Home/SaveFile2", type: "POST", data: formData, processData: false, // 告訴jQuery不要去處理髮送的資料 contentType: false, // 告訴jQuery不要去設定Content-Type要求標頭 success: function (responseText) { alert(responseText); } });});
// c# 後台 : 這個附件上傳是 傳到當前解決方案的並集目錄下的檔案夾,
if(HttpContext.Current.Request.Files.Count>0)
{
string Audit = "被審計單位檔案夾";
string path = HttpContext.Current.Server.MapPath("~/" + Audit + "\\"); //擷取當前檔案夾路徑
string[] temp = path.Split("\\".ToCharArray());
string LevelPath = "";
for (int i = 0; i < temp.Length - 3; i++)
{
LevelPath += temp[i];
LevelPath += "\\";
}
var firstCatalog = LevelPath + Audit;
if (!Directory.Exists(firstCatalog))//如果不存在就建立file檔案夾
{
Directory.CreateDirectory(firstCatalog);
}
if (!Directory.Exists(firstCatalog + "\\" + AuditProjectId))//如果不存在就建立file檔案夾
{
Directory.CreateDirectory(firstCatalog + "\\" + AuditProjectId);
}
var URL = firstCatalog + "\\" + AuditProjectId + "\\";
Guid identifier = Guid.NewGuid();
var httpfile = HttpContext.Current.Request.Files[i]; //擷取當前檔案夾
string suffix = httpfile.FileName.Substring(httpfile.FileName.LastIndexOf(‘.‘)); //擷取檔案夾尾碼
if (httpfile != null)
{
httpfile.SaveAs(URL + identifier + suffix); //儲存檔案流
#region 這些為傳回值, 不需要擇直接忽略
ReviewOpinionFileDTO opmodel = new ReviewOpinionFileDTO();
opmodel.ID = identifier;
opmodel.FileName = httpfile.FileName;
opmodel.FileSize = (httpfile.ContentLength / 1024) + "kb";
opmodel.FileURL = URL + identifier + suffix;
opmodel.FileType = suffix;
#endregion
return opmodel;
}
}
//http://www.cnblogs.com/zhaopei/archive/2017/07/24/upload.html
博主寫的非常詳細,實用
多附件上傳