FileUpload控件PostedFile问题 [英] FileUpload Control PostedFile problem

查看:127
本文介绍了FileUpload控件PostedFile问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有注册页面,我在其中提供用户上传图片的选项



tablename用户



ID int

电子邮件varchar(500)

密码varchar(500)

名称varchar(500)

国家varchar(500)

LastLogin日期时间

RegisterDate datetime

描述Varchar(500)

ImageName varchar (500)允许空值



当我上传图像时图像被正确上传但我没有将验证器设置为图像上传控件允许imageName列中的空值

当用户没有上传图片时出现错误



StartIndex不能小于零。



代码在这里



i have the register page in which i give the option to user upload the image

tablename User

ID int
Email varchar(500)
Password varchar(500)
Name varchar(500)
Country varchar(500)
LastLogin datetime
RegisterDate datetime
Description Varchar(500)
ImageName varchar(500) allow nulls

when i upload the image image is correctly uploaded but i not set the validator to image upload control allow nulls in imageName Column
when user not upload the image error comes

StartIndex cannot be less than zero.

code is here

protected void Button1_Click(object sender, EventArgs e)
    {
        string chkUser = "Select * FROM [User] where Email='" + TextBox2.Text + "'";
        dt = dbClass.ConnectDataBaseReturnDT(chkUser);
        if (dt.Rows.Count > 0)
        {
            Response.Write("<script language='javascript'>alert( 'Already Exsist ' )</script>");
        }
        else
        {
            if (UploadUserPhoto.PostedFile != null)
            {
                string myMap = MapPath("~/").ToLower();
                Random r = new Random();
                int next = r.Next();
                string ImageName = UploadUserPhoto.PostedFile.FileName;
                // ToSaveImageName = DateTime.Now.ToString("yyyy-MM-ddTmm:hh:ss");
                //ToSaveImageName.Replace('-', '1');
                //ToSaveImageName.Replace(':', '2');
                //Directory.CreateDirectory(myMap + ToSaveImageName);


                sImageFileExtension = ImageName.Substring(ImageName.LastIndexOf(".")).ToLower();//error is here when i not upload the image 

                if (sImageFileExtension == ".gif" || sImageFileExtension == ".png" || sImageFileExtension == ".jpg" || sImageFileExtension == ".jpeg" || sImageFileExtension == ".bmp")
                {
                    string ImageSaveURL = myMap + "UserImage/" + next + sImageFileExtension;

                    try
                    {
                        UploadUserPhoto.PostedFile.SaveAs(ImageSaveURL);
                        string RegisterQuery = "INSERT INTO [User] (Email,Password,Name,Country,Gender,Month,Date,Year,Description,ImageName) VALUES('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + DropDownList1.Text + "','" + DropDownList3.Text + "','" + DropDownList2.Text + "','" + DropDownList4.Text + "','" + TextBox1.Text + "','" + next + sImageFileExtension + "')";
                        dbClass.ConnectDataBaseToInsert(RegisterQuery);
                        Response.Redirect("~/Login.aspx");
                    }
                    catch (Exception ex)
                    {

                    }
                }

                else
                {

                }

            }


            else
            {
                ToSaveImageName = "No";
                sImageFileExtension = "Image";
            }
        }





忽略HTML ...选项已禁用,代码出错 ................替换为更具描述性的主题 - OriginalGriff [/ edit]



[edit]"Ignore HTML..." option disabled, "error in code................" replaced with more descriptive subject - OriginalGriff[/edit]

推荐答案

而不是检查

Instead of checking
if (UploadUserPhoto.PostedFile != null)



尝试使用


Try using

if (UploadUserPhoto.HasFile)

相反。



MSDN [ ^ ]





线路在我上传图像时很有用,但我在我的注册页面中解释我已经给用户选择了他/她是否上传图像。





不,不是。

Instead.

MSDN[^]


"line is useful when i uploadimage but i aslo explain in my register page i have give the option to user he/she upload image or not."


No, it isn't.

if (UploadUserPhoto.PostedFile != null)

将始终执行下一个语句,因为 FileUpload.PostedFile 永远不会为null,无论用户是否上传了文件,所以 if 条件评估为

will always execute the next statement because FileUpload.PostedFile is never null, whether the user uploaded a file or not, so the if condition evaluates to

if (true)

另一方面,

On the other hand,

if (UploadUserPhoto.HasFile)

仅当用户成功上传文件时才会成立,因此文件名可用。

Will be true only if the user has sucessfully uploaded a file, and so a file name is available.


在ASP.NET中上传和下载文件 [ ^ ]



可以帮助你。
File Upload and Download in ASP.NET[^]

May be this can help you.


string ImageSaveURL = myMap + "UserImage/" + next + sImageFileExtension;

                   try
                   {
                       UploadUserPhoto.PostedFile.SaveAs(ImageSaveURL);
                       string RegisterQuery = "INSERT INTO [User] (Email,Password,Name,Country,Gender,Month,Date,Year,Description,ImageName) VALUES('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + DropDownList1.Text + "','" + DropDownList3.Text + "','" + DropDownList2.Text + "','" + DropDownList4.Text + "','" + TextBox1.Text + "','" + next + sImageFileExtension + "')";







IN这个你有像ImageSaveURL一样的字符串,但是上传它的文件扩展名到数据库,它是如何工作的!!!!!



我们只需将




IN This u had make string like ImageSaveURL,, but uploading its file extension to database ,how it works!!!!!

Let just change

sImageFileExtension

更改为ImageSaveURL



to ImageSaveURL

DropDownList4.Text + "','" + TextBox1.Text + "','" + next + ImageSaveURL+ "')";



喜欢上面。


Like Above.


这篇关于FileUpload控件PostedFile问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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