多附件上傳

來源:互聯網
上載者:User

標籤: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

博主寫的非常詳細,實用

多附件上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.