今天遇到个怪问题,上传几个文件的时候是这样用的,
// 获取所有文件
HttpFileCollection allFiles = request.Files;
想这样使用
foreach (HttpPostedFile file in allFiles) {
// 但是这里报错,说字符串不能转成HttpPostedFile
}
但是这样不行,虽然知道了不行了,但是很想知道到底在什么情况下才能使用foreach,请各位高手在这里讲解一下,谢谢!
// 获取所有文件
HttpFileCollection allFiles = request.Files;
想这样使用
foreach (HttpPostedFile file in allFiles) {
// 但是这里报错,说字符串不能转成HttpPostedFile
}
但是这样不行,虽然知道了不行了,但是很想知道到底在什么情况下才能使用foreach,请各位高手在这里讲解一下,谢谢!
解决方案 »
- 数据库连接方式选择问题
- 报表绑定
- 首页上有很多城市名称的导航,点击后就是显示这个城市的最新信息列表,怎么实现好?
- 知道 opensocial 的请进
- 急救:各位大哥,为什么cdn那边读我公司的服务器上的文件时,后面会有个?号啊,而有些则没有的。
- 这个问题很简单,请大家来分析一下网址
- “xsl”是未声明的命名空间。这个错误是怎么造成的啊,请看看我的代码,谢谢
- 请问怎么导入一个数据库?用sqlserver2000
- 如何用javascript调用服务器控件checkboxlist中的每一项的text?
- 请问win2003中的IIS中设置的虚拟站点下的文件夹都是默认只读的,怎么能修改它为不是只读?
- asp.net 中怎么在源码中设置sqldatasource.selectcommand为一变量
- asp.net站点上,是如何实现现场直播?
想这样使用
foreach (HttpPostedFile file in allFiles) { 这样是没有问题的你的“// 但是这里报错,说字符串不能转成HttpPostedFile ”
代码是什么?
// 但是这里报错,说字符串不能转成HttpPostedFile
}
应该是你循环里的代码有错,是不是把file 赋值给一个字符串变量了?foreach (HttpPostedFile file in allFiles) 这是没错的,仔细检查下代码
查过了,确实是那个地方出问题了,如果改成
无法将类型为“System.String”的对象强制转换为类型“System.Web.HttpPostedFile”。
完整代码
public static string[] PutFileToFolder(HttpRequest request)
{
// 设置文件存放路径
string folderPath = HttpContext.Current.Server.MapPath(string.Format("~/{0}/{1}/", STORE_FILE_PATH, DateTime.Now.ToString("yyyyMM"))); // 文件不存在
if (System.IO.Directory.Exists(folderPath) == false)
System.IO.Directory.CreateDirectory(folderPath); // 文件存放数组
ArrayList filelist = new ArrayList(); // 获取所有文件
HttpFileCollection allFiles = request.Files;
for (int i = 0; i < allFiles.Count; i++) // 这里不出错
{
}
foreach (HttpPostedFile myfiles in allFiles) { // 这里出错
}
////STORE_FILE_PATH
//string filePath = string.Empty;
//string fileExt = string.Empty;
//for (int i = 0; i < allFiles.Count; i++)
//{
// HttpPostedFile file = allFiles[i]; // // 空文件
// if (file.ContentLength == 0) continue;
// fileExt = System.IO.Path.GetExtension(file.FileName); // // 设置文件路径,名字,后缀
// filePath = string.Format("{0}{1}{2}", folderPath, DateTime.Now.Ticks.ToString(), fileExt); // // 保存文件
// file.SaveAs(filePath); // // 将路径加入列表
// filelist.Add(filePath);
//}
return new string[] { };
}
HttpPostedFile file = allFiles[i];
HttpRequest req = this.Request;
HttpFileCollection allFiles = req.Files;
foreach (HttpPostedFile file in allFiles)
{
}
没有任何问题,不知道楼主问题出在哪。
In C#, it is not strictly necessary for a collection class to inherit from IEnumerable and IEnumerator in order to be compatible with foreach; as long as the class has the required GetEnumerator, MoveNext, Reset, and Current members, it will work with foreach.
只要类实现了GetEnumerator, MoveNext, Reset, Current四个方法和属性就可以了,不一定说要实现IEnumerable 和IEnumerator 接口。而
HttpFileCollection 反射出来的结果为下面的内容
public sealed class HttpFileCollection : NameObjectCollectionBase
{......}
而NameObjectCollectionBase实现了ICollection, IEnumerable, ISerializable, IDeserializationCallback实现了四个接口,显然这符合foreach的要求.......
说类型不对,是因为Current属性的类型无法转换为要求的类型,你可以试试把string换成HttpPostedFile看看,我看应该可以了。
前台aspx
<div id="divFilesPanel">
<ul>
<li><asp:FileUpload ID="myfile" runat="server" /></li>
</ul>
<div><input type="button" id="addUploadFile" value="添加上传控件" /> <asp:Button ID="btnUploadFile" runat="server" Text="开始上传" OnClick="btnUploadFile_Click" /></div>
</div>后台cs
protected void btnUploadFile_Click(object sender, EventArgs e)
{
foreach (HttpPostedFile file in Request.Files)// 无法将类型为“System.String”的对象强制转换为类型“System.Web.HttpPostedFile”。
{ }
// WebUtility.PutFileToFolder(Request);
}
还出那个问题!
{
/**////'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
用for循环就不会,晕
The foreach statement repeats a group of embedded statements for each element in an array or an object collection that implements the System.Collections..::.IEnumerable or ......
HttpFileCollection应该没有实现IEnumerable接口吧!
应该是实现了啊,youbl 他说他不报错...真的不知道是什么原因了
我的目标是 ----> ^_^
你把你的项目打包,发出来给我看看
youbl@126.com
如果写成
foreach (string key in Request.Files)
{
HttpPostedFile file = Request.Files[key];
或者
HttpPostedFile file = Request.Files.Get(key);
...
}
就可以
真是奇怪啊,不能理解,如有高手看到,还望给予讲解,谢谢