前言:文中所讲仅为个人学习使用过程中的一些经验和想法,望多提意见。
一,实现上传APK文件的功能
界面cs.html代码:
<strong> </strong><span style="font-weight: bold; white-space: pre;"> </span>@*enctype= "multipart/form-data"是必需有的,否则action接收不到相应的file*@
@using (Html.BeginForm("AddVersion", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<label>VerName:</label>
@Html.TextBox("VerName", "", new { style="width:100%;"});
<br/>
<label>VerCode:</label>
@Html.TextBox("VerCode", "", new { style = "width:100%;"});
<br/>
<label>DownLoadUrl:</label>
@Html.TextBox("DownLoadUrl", "", new { style = "width:100%;"});
<br/>
<label>DesCription:</label>
@Html.TextBox("DesCription", "", new { style="width:100%;"});
<br/>
<span>文件上传</span>
<br>
<input type="file" name ="file" />
<br/>
<input type="submit" value="提交"/>
}
controller
中的代码:
<span style="white-space:pre"> </span>if (file == null)
{
return Content("没有文件!", "text/plain");
}
var fileName = Path.Combine(Request.MapPath("~/Upload/App"), Path.GetFileName(file.FileName));
try
{
file.SaveAs(fileName);
//tm.AttachmentPath = fileName;//得到全部model信息
///tm.AttachmentPath = "../upload/" + Path.GetFileName(file.FileName);
//return Content("上传成功!", "text/plain");
// return RedirectToAction("Show", tm);
return RedirectToAction("EditVersion");
}
catch
{
return Content("上传异常 !", "text/plain");
}
这样就可以实现文件的上传功能,没有使用JQuery,相对容易点。
(注:在这里上传文件有个问题,就是asp.net默认的最大上传文件是4M,运行超时为90s。解决这个问题我的方法是:修改web.config文件改默认值,添加这样一段代码:
<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
</system.web>
<configuration>
更多解决办法可见:http://www.cnblogs.com/akwwl/p/3573666.html
)
二,实现APK文件的下载
在ASP.net中直接把.apk文件放到/Upload/App路径下,然后发布完了以后,通过http://xxx/Upload/App/xx.apk是访问不了的。解决思路:将asp.net mvc路由忽略.apk文件请求URL映射
具体方法:
在web.config中:
<configuration>
<configSections>
...
<section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" /><!--加入此行-->
</configSections>
...
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" /><!--加入此行-->
</httpModules>
...
</system.web>
</configuration>
在App_Start/RouteConfig.cs文件中
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.apk");// 加入此行
...
}
}
在IIS中配置MIME文件映射
运行->inetmgr
点确定
点中间的:MIME类型
输入需要配置的参数就可以了