多图片上传至文件上传和保存到数据库asp.net [英] Multiple image uploads through file upload and saving to database asp.net

查看:749
本文介绍了多图片上传至文件上传和保存到数据库asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我想选择多个图像,并将它们保存在asp.net的数据库。
我不希望使用任何插件或任何类型的脚本



这是设计师页:

 < ASP:文件上传的AllowMultiple =真正的ID =fileuploadimages=服务器/> 
< ASP:按钮=服务器ID =btnUpload的CssClass =btnStyle文本=上传图片
的OnClick =btnUpload_Click/>



我迄今按钮上传活动做到了这一点。它所做的是,它可以让我选择多个文件,就像如果我选择3张图片,它在我的数据库三次保存第一个文件和文件夹图片中曾将其保存,而不是保存所有三种。

 保护无效btnUpload_Click(对象发件人,EventArgs五)
{
如果(fileuploadimages.HasFile ==假)
{
ScriptManager.RegisterStartupScript(页,Page.GetType(),重点,<脚本>警报('没有文件上传。')< / SCRIPT>中,FALSE);
}
,否则
{
的foreach(在fileuploadimages.PostedFiles var文件)
{
字符串文件名= Path.GetFileName(fileuploadimages.PostedFile.FileName) ;

fileuploadimages.SaveAs(使用Server.Mappath(../图片/+文件名));

的SqlCommand CMD =新的SqlCommand(插入到
EventPageView(EVENT_NAME,Event_Text,IMAGE_PATH)
值(@事件名称,@ EventText,@的ImagePath),conn);在

cmd.Parameters.AddWithValue(@的ImagePath,文件名);
cmd.Parameters.AddWithValue(@事件名称,txtEventName.Text);
cmd.Parameters.AddWithValue(@ EventText,txtEnterDesc.Text);
conn.Open();
cmd.ExecuteNonQuery();

conn.Close();
BindDataList();
}
}
}


解决方案

更改这两行的行:

 字符串文件名= Path.GetFileName(fileuploadimages.PostedFile.FileName); 

fileuploadimages.SaveAs(使用Server.Mappath(../图片/+文件名));



 字符串文件名= Path.GetFileName(file.FileName); 

file.SaveAs(使用Server.Mappath(../图片/+文件名));



我希望这会有所帮助。因为你必须使用对象文件


Okay, so I'm trying to select multiple images and save them in a database in asp.net. I don't want to use any plugins or any kind of scripts.

This is the designer page:

<asp:FileUpload AllowMultiple="true"  ID="fileuploadimages" runat="server" />
<asp:Button runat="server" ID="btnUpload" CssClass="btnStyle" Text="Upload Image"
 OnClick="btnUpload_Click" />

I have done this so far on the button upload event. What it does is that it allows me to select more than one file, like if I select 3 images, it saves the first file thrice in my database and saves it once in the folder "Pictures" instead of saving all three.

protected void btnUpload_Click(object sender, EventArgs e)
{
    if (fileuploadimages.HasFile == false)
    {
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", "<script>alert('No File Uploaded.')</script>", false);
    }
    else    
    {
        foreach (var file in fileuploadimages.PostedFiles)
        {
            string filename =Path.GetFileName(fileuploadimages.PostedFile.FileName);

            fileuploadimages.SaveAs(Server.MapPath("../Pictures/" + filename));  

            SqlCommand cmd = new SqlCommand("Insert into
            EventPageView(Event_name,Event_Text,Image_Path)
            values(@EventName,@EventText,@ImagePath)", conn);

            cmd.Parameters.AddWithValue("@ImagePath", filename);
            cmd.Parameters.AddWithValue("@EventName", txtEventName.Text);
            cmd.Parameters.AddWithValue("@EventText", txtEnterDesc.Text);
            conn.Open();
            cmd.ExecuteNonQuery();

            conn.Close();
            BindDataList();
        }    
    }
}

解决方案

Change those two lines line:

string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);

fileuploadimages.SaveAs(Server.MapPath("../Pictures/" + filename));

to

string filename = Path.GetFileName(file.FileName);

file.SaveAs(Server.MapPath("../Pictures/" + filename));

I hope it will help. Because you must use the object file

这篇关于多图片上传至文件上传和保存到数据库asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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