在asp.net中访问服务器端的输入类型文件
发布时间:2020-05-25 04:38:59 所属栏目:asp.Net 来源:互联网
导读:我正在使用 input type =“file”/标记以将文件上载到服务器.如何在服务器端访问该文件并将其存储在服务器上? (该文件是图像文件) 客户端代码是: form id=form1 action=PhotoStore.aspx enctype=multipart/form-data div input type=file id=file o
|
我正在使用< input type =“file”/>标记以将文件上载到服务器.如何在服务器端访问该文件并将其存储在服务器上? (该文件是图像文件) 客户端代码是: <form id="form1" action="PhotoStore.aspx" enctype="multipart/form-data">
<div>
<input type="file" id="file" onchange="preview(this)" />
<input type="submit" />
</div>
</form>
Photostore.aspx.cs有 protected void Page_Load(object sender,EventArgs e)
{
int index = 1;
foreach (HttpPostedFile postedFile in Request.Files)
{
int contentLength = postedFile.ContentLength;
string contentType = postedFile.ContentType;
string fileName = postedFile.FileName;
postedFile.SaveAs(@"c:testfile" + index + ".tmp");
index++;
}
}
我尝试上传jpg文件.无法查看已保存的文件.出了什么问题? 解决方法您需要添加id和runat =“server”属性,如下所示:<input type="file" id="MyFileUpload" runat="server" /> 然后,在服务器端,您将可以访问控件的 int contentLength = MyFileUpload.PostedFile.ContentLength; string contentType = MyFileUpload.PostedFile.ContentType; string fileName = MyFileUpload.PostedFile.FileName; MyFileUpload.PostedFile.Save(@"c:test.tmp"); 或者,您可以使用 int index = 1;
foreach (HttpPostedFile postedFile in Request.Files)
{
int contentLength = postedFile.ContentLength;
string contentType = postedFile.ContentType;
string fileName = postedFile.FileName;
postedFile.Save(@"c:test" + index + ".tmp");
index++;
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net中的GridView分页问题
- asp.net-mvc – 为什么IIS没有清理池回收导致网站内存异常的
- asp.net-mvc – Razor视图与部分视图
- asp.net – 自我跟踪实体vs POCO实体
- asp.net-mvc – ASP.NET MVC JavaScript路由
- asp.net-mvc – ASP.NET MVC:使浏览器缓存图像从动作
- asp.net-mvc-3 – SignalR依赖注入问题
- asp.net – TeamCity可以使用sln2008构建运行程序发布Web项
- asp.net – 通过MSMQ分离Web和数据库层是必要的还是过度的?
- asp.net webservice返回json的方法
推荐文章
站长推荐
- asp.net – 您没有使用批量加载语句的权限
- asp.net – 循环访问复选框列表
- asp.net – 如何使用ReportService2010命名空间呈
- 如何在服务器上安装ASP.NET MVC 5?
- Asp.net实现MVC处理文件的上传下载功能实例教程
- asp.net – sql server报告服务和其他报告工具之
- asp.net-mvc-4 – 使用KNOCKOUT.JS和ASP.NET MVC
- asp.net-mvc – AOP vs MVC FilterAttributes vs
- asp.net-identity – IIdentity.Name与IIdentity
- asp.net-mvc-3 – 找不到视图’错误’或其主人
热点阅读
