如何检查上传的文件格式是否正确? [英] How do I check if the uploaded file has the right format?

查看:371
本文介绍了如何检查上传的文件格式是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用c#和asp.net



我创建了一个网页,在其中输入您的信息以便提交。在我的页面上也有一个文件上传:< asp:FileUpload ID =FileUploadPassfotorunat =server/> - 检查是否有上传的东西。像这样:

pre $ if $($ File $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ H $% PostedFile;
using(BinaryReader binaryReader = new BinaryReader(file.InputStream))
{
lehrling.passfoto = binaryReader.ReadBytes(file.ContentLength);
}
LabelPassfotoError.Visible = false;
}
else
{
LabelPassfotoError.Visible = true;
LabelError.Visible = true;



$ b $ p
$ b

它的作用是:正如我所说的,它检查是否有上传。如果没有上传,ErrorLabel会显示,所以用户知道他忘了上传。



我也想检查,如果上传的文件是图像。为了更清楚我只想接受.jpg / .bmp和.gif。如果错误的格式上传,我想显示我的ErrorLabel以及。

我真的不知道我应该怎么做,你能帮我吗?谢谢

解决方案

  protected void Button1_Click(object sender,EventArgs e)
{
string strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string strFileWithoutExt = Path.GetFileNameWithoutExtension(strFileName);
string strExtension = Path.GetExtension(strFileName);
if(strExtension ==.jpg|| strExtension ==.bmp|| strExtension ==.gif)
{
string strImageFolder =〜/ YourFilePath / ;
if(!Directory.Exists(Server.MapPath(strImageFolder)))
Directory.CreateDirectory(Server.MapPath(strImageFolder));
string _strPath = Server.MapPath(strImageFolder)+ strFileName;
FileUpload1.PostedFile.SaveAs(_strPath);
Label1.Text =上传状态:上传文件。
}
else
Label1.Text =上传状态:只允许使用.jpg,.bmp和.gif文件!;

$ / code>

希望它的帮助更多....


I work with c# and asp.net

I created a webpage with a web form where you enter your information in order to submit it. There is also a file upload on my page: <asp:FileUpload ID="FileUploadPassfoto" runat="server"/> In my c# code behind i coded a IF-Loop which checks if something got uploaded. Like this:

if (FileUploadPassfoto.HasFile == true)
        {
            HttpPostedFile file = FileUploadPassfoto.PostedFile;
            using (BinaryReader binaryReader = new BinaryReader(file.InputStream))
            {
                lehrling.passfoto = binaryReader.ReadBytes(file.ContentLength);
            }
            LabelPassfotoError.Visible = false;
        }
        else
        {
            LabelPassfotoError.Visible = true;
            LabelError.Visible = true;
        }

What it does is: As i said it checks if something got uploaded. If nothing got uploaded a ErrorLabel will be shown so the user knows he forgot to upload.

What i want to check too, is if the uploaded file is a image. To be more clear i only want to accept .jpg/.bmp and .gif. If a wrong format gets uploaded i want to display my ErrorLabel as well.

I dont really know how i should do this, can you please help me? Thank you

解决方案

    protected void Button1_Click(object sender, EventArgs e)
    {
        string strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string strFileWithoutExt = Path.GetFileNameWithoutExtension(strFileName);
        string strExtension = Path.GetExtension(strFileName);
        if (strExtension == ".jpg" || strExtension == ".bmp" || strExtension == ".gif")
        {
            string strImageFolder = "~/YourFilePath/";
            if (!Directory.Exists(Server.MapPath(strImageFolder)))
                Directory.CreateDirectory(Server.MapPath(strImageFolder));
            string _strPath = Server.MapPath(strImageFolder) + strFileName;
            FileUpload1.PostedFile.SaveAs(_strPath);
            Label1.Text = "Upload status: File uploaded.";
        }
        else
            Label1.Text = "Upload status: only .jpg,.bmp and .gif file are allowed!";
    }

Hope Its Help You much more....

这篇关于如何检查上传的文件格式是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆