C#kendoUpload点击Submit再JSAJAX上传后台,文件转字节储存数据库 [数据库教程]
页面:
1@using (Html.BeginForm("Update", "Controller", FormMethod.Post, 2new { id = "Form", enctype = "multipart/form-data" })) 3{ 45<div class="modal-body">
6
7@(Html.Hidden("ID"))
8
9<div class="form-group m-form__group row text-center">
10<label class="col-lg-2 col-form-label">File:</label>
11<div class="col-lg-8">
12<input name="files" id="files" type="file" aria-label="files"/>
13
14</div>
15</div>
16
17<div class="form-group m-form__group row text-center">
18<label class="col-lg-2 col-form-label">Description:</label>
19<div class="col-lg-8">
20<textarea class="form-control" rows="4" maxlength="250" name="Description" id="Description" style="min-width:200px"></textarea>
21</div>
22</div>
23
24<input class="btn btn-success" type="submit" id="submit" value="submit"/>
25
26</div>
27
28
29
30 }
JS:
1 <script>2
3 $(document).ready(function () {
4 //Kendoui 绑定
5 $("#files").kendoUpload({
6 multiple: false,
7 select: function (e) {
8 console.log("size:" + e.files[0].size)
9 console.log("extension:" + e.files[0].extension)
10},
11validation: {
12 allowedExtensions: [".jpg", ".png", ".pdf", ".docx", ".xlsx", ".xls", ".doc", ".ppt", ".pptx"],
13 maxFileSize: (1048576 * 5),
14
15},
16});
17});
18
19
20 //form 提交时触发,‘swal‘是一个比较靓仔的弹窗
21 $(‘form‘).submit(function (event) {
22var fileData = $("#files").data(‘kendoUpload‘).getFiles();
23if (fileData.length == 0 && $("#FileId").val() == "") {
24swal({
25 title: "Pelase Select File",
26 html: "",
27 type: ‘warning‘,
28 showCancelButton: false,
29 confirmButtonText: "OK",
30});
31returnfalse;
32}
33mApp.blockPage();
34event.preventDefault();
35var formdata = new FormData($(‘#Form‘).get(0));
36$.ajax({
37 type: "POST",
38 url: ‘@Url.Action("Update", "Controller")‘,
39data: formdata,
40 dataType: "json",
41 processData: false,
42 contentType: false,
43 success: function (response) {
44mApp.unblockPage()
45if (response.Success) {
46swal({
47 title: "Submission is completed",
48 html: "",
49 type: ‘warning‘,
50 showCancelButton: false,
51 confirmButtonText: "OK",
52});
53
54 } else {
55swal({
56 title: "Error",
57 html: "",
58 type: ‘warning‘,
59 showCancelButton: false,
60 confirmButtonText: "OK",
61});
62}
63}
64});
65});
66
67}
68
69 </script>
C#:
1publicclass ViewModel 23{
4
5publicint ID{ get; set; }
6
7publicstring Description{ get; set; }
8
9}
10
11[HttpPost]
12
13public ActionResult UpdateDocumentTemplate([Bind(Exclude = null)] ViewModel model, IEnumerable<HttpPostedFileBase> files)
14
15{
16
17foreach (var file in files)
18
19{
20
21if (file != null){
22
23var TempPathFolder ="C:Temporary"
24
25//判断路径是否存在
26
27if (!Directory.Exists(TempPathFolder))
28
29{
30
31 Directory.CreateDirectory(TempPathFolder);//不存在,创建
32
33}
34
35var fileName = Path.GetFileName(file.FileName);
36
37var TempFilePath = TempPathFolder + @"" + fileName;
38
39 file.SaveAs(TempFilePath);//文件保存
40
41//
42
43 DirectoryInfo directory = new DirectoryInfo(TempPathFolder);
44
45 files2 = directory.GetFiles();
46if (files2.Length > 0)
47
48 {
49
50byte[] bytes = System.IO.File.ReadAllBytes(files2[0].FullName);
51
52 fileArr = CompressHelper.Compress(bytes);
53
54 fileFormat = (files2[0].Name).Split(‘.‘)[1];//文件转字节,可存数据库
55
56
57
58 }
59
60}
61
62}
63
64 }
字节转文件:
1[System.Web.Http.HttpGet] 2public HttpResponseMessage eMemoTemplateDownload( ) 3{ 4 HttpResponseMessage result = new HttpResponseMessage(); 5byte[] file;//字节 6var fileName =""; 7if (file != null) 8 { 9 result = new HttpResponseMessage(HttpStatusCode.OK);10 result.Content = new ByteArrayContent(file);11 result.Content.Headers.ContentDisposition =12new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
13 {
14 FileName = fileName
15 };
16return result;
17 }
18
19 }
Form
C# kendoUpload 点击Submit 再 JS AJAX 上传后台,文件转字节储存数据库
以上是 C#kendoUpload点击Submit再JSAJAX上传后台,文件转字节储存数据库 [数据库教程] 的全部内容, 来源链接: utcz.com/z/535302.html