页面:
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
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 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 上传后台,文件转字节储存数据库
原文地址:https://www.cnblogs.com/RenPS/p/14218335.html