错误1 foreach语句无法对“System.Web.HttpPostedFile”类型的变量进行操作,因为“System.Web.HttpPostedFile”不包含“GetEnumerator”的公共定义 [英] Error 1 foreach statement cannot operate on variables of type 'System.Web.HttpPostedFile' because 'System.Web.HttpPostedFile' does not contain a public definition for 'GetEnumerator'
本文介绍了错误1 foreach语句无法对“System.Web.HttpPostedFile”类型的变量进行操作,因为“System.Web.HttpPostedFile”不包含“GetEnumerator”的公共定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void uploadFile_Click(对象发件人,EventArgs e)
{
if (UploadImages.HasFile)
{
foreach (HttpPostedFile myfile in UploadImages.PostedFile)
{
myfile.SaveAs(System.IO.Path.Combine(Server.MapPath( 〜\\uploadfile \ \跨度>),myfile.FileName));
listofuploadedfiles.Text + = String .Format( {0}< br />,myfile.FileName);
}
}
asp.net ....
< asp:FileUpload runat = server ID = UploadImages AllowMultiple = true / >
< asp:Button runat = < SP class =code-keyword> server ID = myfile 文字 = 上传 OnClick = uploadFile_Click / >
< asp:标签 ID = listofuploadedfiles runat = server / >
解决方案
错误消息非常明确:您无法使用For Each这不是一个集合。
属性名称本身就是一个线索:PostedFile
不是多元化的,暗示它包含单个项目,而不是项目集合。请改为使用PostedFiles
属性。
按MSDN [ ^ ],你不能使用每个UploadImages.PostedFile
因为它不是一个集合修改你的代码...
if (UploadImages.HasFile)
{
HttpPostedFile myfile = UploadImages.PostedFile)
myfile.SaveAs(System.IO.Path.Combine(Server) .MapPath( 〜\\uploadfile \\),myfile.FileName)) ;
listofuploadedfiles.Text + = String .Format( {0}< br />,myfile.FileName);
}
更正格式问题。
[/编辑] 的
protected void uploadFile_Click(object sender, EventArgs e)
{
if(UploadImages.HasFile)
{
foreach(HttpPostedFile myfile in UploadImages.PostedFile)
{
myfile.SaveAs(System.IO.Path.Combine(Server.MapPath("~\\uploadfile\\"),myfile.FileName));
listofuploadedfiles.Text += String.Format("{0}<br />", myfile.FileName);
}
}
asp.net....
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
<asp:Button runat="server" ID="myfile" Text="Upload" OnClick="uploadFile_Click" />
<asp:Label ID="listofuploadedfiles" runat="server" />
解决方案
The error message is pretty explicit: you can't use For Each on something that isn't a collection.
The property name itself is a clue: "PostedFile
" is not pluralised, implying it contains a single item, and not a collection of items. Try thePostedFiles
property instead.
As per MSDN[^], you cannot use for each withUploadImages.PostedFile
since it is not a collection modify your code to...
if(UploadImages.HasFile) { HttpPostedFile myfile = UploadImages.PostedFile) myfile.SaveAs(System.IO.Path.Combine(Server.MapPath("~\\uploadfile\\"),myfile.FileName)); listofuploadedfiles.Text += String.Format("{0}<br />", myfile.FileName); }
[Edit member="Tadit"]
Corrected formatting issues.
[/Edit]
这篇关于错误1 foreach语句无法对“System.Web.HttpPostedFile”类型的变量进行操作,因为“System.Web.HttpPostedFile”不包含“GetEnumerator”的公共定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文