MVC3 Upload上传及定义上传的大小

cshtml 页

View Code
 1 @(Html.Telerik().Upload()
2 .Name("attachments")
3 .Multiple(true)
4 .Async(async => async
5 .Save("Save", "Home")
6 .Remove("Remove", "Home")
7 .AutoUpload(false)//是否自动上传
8 )
9 .ClientEvents(events => events
10 .OnSelect("onSelect")
11 .OnUpload("onUpload")
12 .OnSuccess("onSuccess")
13 .OnRemove("onRemove")
14 .OnComplete("onComplete")
15 )
16 )
17
18 <script type="text/javascript">
19 function onUpload(e) {
20
21
22 }
23
24 function onSelect(e) {
25 // Array with information about the uploaded files
26 var files = e.files;
27
28 if (files.length > 5) {
29 alert("Too many files selected!");
30 e.preventDefault();
31 }
32 }
33 function onComplete(e) {
34 // The upload is now idle
35 }
36
37 function onSuccess(e) {
38 //$console.log("Success (" + e.operation + ") :: " + getFileInfo(e));
39 // $('#AttachmentGrid').data('tGrid').ajaxRequest();
40
41 }
42 function onRemove(e) {
43 //$console.log("Remove :: " + getFileInfo(e));
44 }
45
46 </script>

UploadController

Controller Code
 1  public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
2 {
3 // The Name of the Upload component is "attachments"
4 foreach (var file in attachments)
5 {
6 // Some browsers send file names with full path. We only care about the file name.
7 var fileName = Path.GetFileName(file.FileName);
8 var destinationPath = Path.Combine(Server.MapPath("~/Upload"), fileName);
9
10 file.SaveAs(destinationPath);
11 }
12
13 // Return an empty string to signify success
14 return Content("");
15 }
16
17
18 public ActionResult Remove(string[] fileNames)
19 {
20 foreach (var fullName in fileNames)
21 {
22 var fileName = Path.GetFileName(fullName);
23 var physicalPath = Path.Combine(Server.MapPath("~/Upload"), fileName);
24
25 // TODO: Verify user permissions
26
27 if (System.IO.File.Exists(physicalPath))
28 {
29 System.IO.File.Delete(physicalPath);
30 }
31 }
32
33 // Return an empty string to signify success
34 return Content("");
35 }

web.config

<system.web>
<!-- The request length is in kilobytes, execution timeout is in seconds -->
<httpRuntime maxRequestLength="100240" executionTimeout="500" />
</system.web>
  <system.webServer>
<security>
<requestFiltering>
<!-- The content length is in bytes -->
<requestLimits maxAllowedContentLength="150485760"/>
</requestFiltering>
</security>
</system.webServer>

分别在这两个节点加上以上代码 控制文件大小的关键



 

posted @ 2012-02-14 15:20  ミ茹此茹此↘  Views(381)  Comments(0Edit  收藏  举报