码迷,mamicode.com
首页 > 数据库 > 详细

C# kendoUpload 点击Submit 再 JS AJAX 上传后台,文件转字节储存数据库

时间:2021-01-04 11:33:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:turn   cancel   判断   col   set   doc   creat   script   als   

 

页面:

 1 @using (Html.BeginForm("Update", "Controller", FormMethod.Post,
 2 new { id = "Form", enctype = "multipart/form-data" }))
 3 {
 4 
 5 <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 },
11 validation: {
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) {
22 var fileData = $("#files").data(‘kendoUpload‘).getFiles();
23 if (fileData.length == 0 && $("#FileId").val() == "") {
24 swal({
25 title: "Pelase Select  File",
26 html: "",
27 type: ‘warning‘,
28 showCancelButton: false,
29 confirmButtonText: "OK",
30 });
31 return false;
32 }
33 mApp.blockPage();
34 event.preventDefault();
35 var formdata = new FormData($(‘#Form‘).get(0));
36 $.ajax({
37 type: "POST",
38 url: ‘@Url.Action("Update", "Controller")‘,
39 data: formdata,
40 dataType: "json",
41 processData: false,
42 contentType: false,
43 success: function (response) {
44 mApp.unblockPage()
45 if (response.Success) {
46 swal({
47 title: "Submission is completed",
48 html: "",
49 type: ‘warning‘,
50 showCancelButton: false,
51 confirmButtonText: "OK",
52 });
53 
54 } else {
55 swal({
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#:

 1 public class ViewModel
 2 
 3 {
 4 
 5 public int ID{ get; set; }
 6 
 7 public string Description{ get; set; }
 8 
 9 }
10 
11 [HttpPost]
12 
13 public ActionResult UpdateDocumentTemplate([Bind(Exclude = null)] ViewModel model, IEnumerable<HttpPostedFileBase> files)
14 
15 {
16 
17 foreach (var file in files)
18 
19 {
20 
21 if (file != null){
22 
23 var TempPathFolder ="C:\Temporary\"
24 
25 //判断路径是否存在
26 
27  if (!Directory.Exists(TempPathFolder))
28 
29 {
30 
31 Directory.CreateDirectory(TempPathFolder);//不存在,创建
32 
33 }
34 
35 var fileName = Path.GetFileName(file.FileName);
36 
37 var TempFilePath = TempPathFolder + @"\" + fileName;
38 
39 file.SaveAs(TempFilePath);//文件保存
40 
41 //
42 
43 DirectoryInfo directory = new DirectoryInfo(TempPathFolder);
44 
45  files2 = directory.GetFiles();
46  if (files2.Length > 0)
47 
48                         {
49 
50                             byte[] 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]
 2 public HttpResponseMessage eMemoTemplateDownload( )
 3 {
 4  HttpResponseMessage result = new HttpResponseMessage();
 5 byte[] file;//字节
 6 var fileName ="";
 7  if (file != null)
 8             {
 9                 result = new HttpResponseMessage(HttpStatusCode.OK);
10                 result.Content = new ByteArrayContent(file);
11                 result.Content.Headers.ContentDisposition =
12                         new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
13                         {
14                             FileName = fileName
15                         };
16                 return result;
17             }
18 
19 }

 



 

Form

C# kendoUpload 点击Submit 再 JS AJAX 上传后台,文件转字节储存数据库

标签:turn   cancel   判断   col   set   doc   creat   script   als   

原文地址:https://www.cnblogs.com/RenPS/p/14218335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!