新手跪求!怎样限制用户上传的csv格式文件

cpm205 2009-04-21 01:04:32
<tr>
<td><input id="UploadTheFile" type="file" runat="server" NAME="UploadTheFile"></td>
</tr>

跪求怎样使用这个htmlInputFile才能检查用户上传的csv格式的文件是否只包含2个column, 而且第一行必须是column name "Code","Quantity".
...全文
447 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
makun0624 2009-04-21
  • 打赏
  • 举报
回复
学习了`
皓月明 2009-04-21
  • 打赏
  • 举报
回复
获取扩展名判断啊
cpm205 2009-04-21
  • 打赏
  • 举报
回复
还是赶快揭帖把,要不分都不够给的
陌上花花 2009-04-21
  • 打赏
  • 举报
回复
帮顶下
_see_you_again_ 2009-04-21
  • 打赏
  • 举报
回复
两个问题一并解决
    public void CheckCsvAndSendMail()
{
if (UploadTheFile.HasFile)
{
StreamReader sr = new StreamReader(UploadTheFile.PostedFile.InputStream);
if (sr.ReadLine().ToLower() == "code,quantity")
{
UploadTheFile.PostedFile.InputStream.Position = 0;
MailMessage mail = new MailMessage();
mail.Subject = "test";
mail.From = new MailAddress("test@test.com");
mail.To.Add(new MailAddress("test@test.com"));
mail.Attachments.Add(new Attachment(UploadTheFile.PostedFile.InputStream, UploadTheFile.FileName));
SmtpClient smtp = new SmtpClient("smtp.test.net");
smtp.Send(mail);
}
else
{
throw new Exception("Does not a valid csv file!");
}
}
}
markdandan 2009-04-21
  • 打赏
  • 举报
回复
5楼对的啊
春天的气息 2009-04-21
  • 打赏
  • 举报
回复
顶10楼,GOOD
cpm205 2009-04-21
  • 打赏
  • 举报
回复
俄的神亚,这么多人回复,小弟不胜感激,保证给建议的人都有分数。 既然这么多人关心,我就把遇到的问题具体说明一下。
用户上传一个csv文件,我希望实现以下具体步骤
1.检查csv文件是不是只有2个cloumn,而且column name分别是“code"和“Quantity"
2.把这个csv文件用email发出。
关于第2各问题我想出两个办法
1.将用户上传的文件暂时保留在服务器的临时文件夹中,一旦email被发送成功,删除该文件。我感觉可能
要涉及到权限问题,不知道是不是所有用户都有权限把文件上传到服务器的临时文件夹中。
2.直接发送,但是要将csv文件读取到什么地方就不知道了。

悔说话的哑巴 2009-04-21
  • 打赏
  • 举报
回复
先获取,在判断:
StreamReader reader = new StreamReader(Request.Files[0].InputStream);
string firstLine = reader.ReadLine();
reader.Close();
zhangmeeno 2009-04-21
  • 打赏
  • 举报
回复
LS的方法很使用,这句:“//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 208207是word excel
”非常好用 O(∩_∩)O哈哈~
热学沸腾56 2009-04-21
  • 打赏
  • 举报
回复
学习...
zzxap 2009-04-21
  • 打赏
  • 举报
回复
[code=C#]
if (IsAllowedExtension(FileUpload1))
{
string path = Server.MapPath("~/picture/");
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
else
{
Response.Write("<script>alert('您只能上传jpg或者gif图片');</script>");
}


public static bool IsAllowedExtension(FileUpload hifile)
{
System.IO.FileStream fs = new System.IO.FileStream(hifile.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();

}
catch
{

}
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar 208207是word excel

{
return true;
}
else
{
return false;
}

}


CSV我不知道是什么代码,你上传一个csv文件 输出fileclass是什么。然后判断用这个代码判断

[/CODE]
ixxu9394 2009-04-21
  • 打赏
  • 举报
回复
与FileUpload有关的其它东东:
PostedFile的子属性:

(1)FileName:取得上传文件的完整名(包括路径);
(2)ContentType:上传文件的MIME类型>>(先举个例子先:jpg的MIME的类型为“image/pjpeg”);
(3)ContentLength:上传文件的大小,单位是字节Byte;


(6)文件的MIME类型:

string FileContentType = _CanUploadFilePattern.ToLower();
FileContentType = FileContentType.Replace("*.gif", "image/gif");
FileContentType = FileContentType.Replace("*.jpg", "image/pjpeg");
FileContentType = FileContentType.Replace("*.bmp", "image/bmp");
FileContentType = FileContentType.Replace("*.avi", "video/avi|video/msvideo|video/x-msvideo");
FileContentType = FileContentType.Replace("*.afl", "video/animaflex");
FileContentType = FileContentType.Replace("*.asf", "video/x-ms-asf");
FileContentType = FileContentType.Replace("*.arj", "application/arj");
FileContentType = FileContentType.Replace("*.rar", "application/x-rar-compressed|application/octet-stream");
FileContentType = FileContentType.Replace("*.zip", "application/zip|multipart/x-zip|application/x-compressed|application/x-zip-compressed");
FileContentType = FileContentType.Replace("*.gz", "application/x-gzip-compressed|application/x-compressed");
FileContentType = FileContentType.Replace("*.wav", "audio/wav|audio/x-wav");
FileContentType = FileContentType.Replace("*.mpg", "audio/mpeg|video/mpeg");
FileContentType = FileContentType.Replace("*.mp3", "video/x-mpeg|audio/mpeg3|audio/x-mpeg-3|video/mpeg");
FileContentType = FileContentType.Replace("*.mpe", "video/mpeg");
FileContentType = FileContentType.Replace("*.pdf", "application/pdf");
FileContentType = FileContentType.Replace("*.rm", "audio/x-pn-realaudio|application/vnd.rn-realmedia");
FileContentType = FileContentType.Replace("*.rmi", "audio/mid");
FileContentType = FileContentType.Replace("*.swf", "application/x-shockwave-flash");
FileContentType = FileContentType.Replace("*.txt", "text/plain");

以上代码原版引自E道通,并有样板控件下载,原网站上有几篇关于文件上传的文章.

zsuswy 2009-04-21
  • 打赏
  • 举报
回复
补充一下,从SCVFileName文件取得相关DataSet的代码.
string strConn = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="{0}";Extended Properties="Excel 12.0";HDR=YES;IMEX=1", SCVFileName);
OleDbDataAdapter ExcelDA = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet ds = new DataSet();
try
{
ExcelDA.Fill(ds, "ExcelInfo");
return ds;
}

上面链接字符串中的Extended Properties="Excel 12.0"需要根据你自己的Excel版本做选择。
wuyq11 2009-04-21
  • 打赏
  • 举报
回复
上传CSV文件后,打开csv 文件实现判断
参考
cpm205 2009-04-21
  • 打赏
  • 举报
回复
我就是不知道怎样才能把csv读取到dataset或是datatable中,后面的怎么判断都知道怎么写
路人乙e 2009-04-21
  • 打赏
  • 举报
回复
StreamReader reader = new StreamReader(Request.Files[0].InputStream);
string firstLine = reader.ReadLine();
reader.Close();

firstLine就是文件的第一行,获取后对firstLine进行判断即可
zsuswy 2009-04-21
  • 打赏
  • 举报
回复
在上传了svc文件后,可以把它当成一个数据源读取到dataset中,指定数据源中第一行为列名,然后判断列名就可以了。
lgaimin 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 goldxinx 的回复:]
C# code
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
string[] rd = Request.Form[4].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
string[] valuestr = ddlAlbum.SelectedValue.Trim().Split(';');
string albumid = valuestr[1];//相册ID

[/Quote]


UP;mark
cpm205 2009-04-21
  • 打赏
  • 举报
回复
谢谢了,不过我知道怎么检查上传文件是否是csv文件,目前的问题是怎样才能检查csv文件是不是只有2个cloumn,而且column name分别是“code"和“Quantity"
加载更多回复(1)

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧