【问题标题】:save an image which reside on client device to the database on server将驻留在客户端设备上的图像保存到服务器上的数据库中
【发布时间】:2014-03-19 12:16:05
【问题描述】:

我想使用 ASP.NET 执行一个简单的操作。我希望用户选择尽可能多的图像并将这些图像保存到服务器上运行的数据库中。每次用户想要添加新图像时,他单击“添加”以添加新图像。图像具有附带的属性,例如拍摄位置和时间。位置通过文本框输入,日期通过日期选择器输入。

问题在于将图像保存到服务器。

是否有任何控件可以执行此任务?

我曾尝试使用 Upload 控件和 Devexpress ASPxUpload 控件,但无济于事。上传控件还要求我先将图像上传到服务器,然后再将其保存到数据库。

我尝试将代表图像及其日期和位置的控件实现为用户控件,但是每当我单击上传按钮时,都不会调用该事件

这是用户控件的代码

    public partial class ucVerificationItem : System.Web.UI.UserControl
{
    public byte[] data { set; get; }
    public byte[] Photo { get { return data; } }
    //ASPxFormLayout1_E3.Text is a Text Box which has the verification text
    public string Verify { get { return ASPxFormLayout1_E3.Text; } }
    //ASPxFormLayout1_E4.Text is a Text Box which has the caption text
    public string Caption { get { return ASPxFormLayout1_E4.Text; } }
    //ASPxFormLayout1_E5.Date is a DateEdit which has the date
    public DateTime Date { get { return ASPxFormLayout1_E5.Date; } }

    // to expose the button to the page from which it is gonna be called
    public ASPxButton Button { get { return ASPxButton1; } }
    //to expose the uplaod control
    public ASPxUploadControl UploadControl { get { return ASPxFormLayout1_E2; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        ASPxFormLayout1_E2.UploadedFiles[0].SaveAs(Server.MapPath("~\\temp\\"+ASPxFormLayout1_E2.UploadedFiles[0].FileName));
    }

    protected void ASPxFormLayout1_E2_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
    {
        ASPxFormLayout1_E2.UploadedFiles[0].PostedFile.InputStream.Read(data, 0, ASPxFormLayout1_E2.PostedFile.ContentLength);
    }
}

这是调用控件的页面代码

        protected void Page_Init(object sender, EventArgs e) {
        if (IsPostBack)
        {
            if (Session["Photos"] == null)
            {
                List<ucVerificationItem> list = new List<ucVerificationItem>();
                Session["Photos"] = list;
            }
            foreach (ucVerificationItem item in Session["Photos"] as List<ucVerificationItem>)
            {
                PlaceHolder1.Controls.Add(item);
            }
        }       
    }

        protected void addPhotoASPxButton_Click(object sender, EventArgs e)
    {
        ucVerificationItem item = (ucVerificationItem)Page.LoadControl("~/Company/Forms/ucVerificationItem.ascx");
        (Session["Photos"] as List<ucVerificationItem>).Add(item);
        PlaceHolder1.Controls.Add(item);
    }

希望这足够清楚

【问题讨论】:

    标签: c# asp.net database image


    【解决方案1】:

    位置是通过文本框输入的

    没有。服务器无法访问该位置,特别是如果它被编码为来自客户端的本地路径(即 C:\blablabla)

    使用 FIleUploadControl 并将二进制数据作为请求的一部分发送到服务器。

    除了有很多方法可以让 jquery 等变得更好之外,bsi 文件上传是 HTML 的核心部分,你应该在阅读一些 HTML 文档时了解它。

    http://webdesign.about.com/od/htmltags/p/input-file-tag.htm

    谈论它。

    核心语法是:

    <form enctype="multipart/form-data">
    <input type="file" accept="image/jpg,image/gif">
    </form>
    
    • 您必须将 enctype 设置为 multipart,否则无法发送二进制数据,并且必须是 POST - 没有 GET 请求。
    • Input type=file 是浏览器知道它应该发布 fike 的方式。

    除此之外,您还可以做很多 jquery(或在互联网上获取脚本),允许拖放和其他非常好的事情 - 例如拖放多个文件并将它们作为单独的请求逐个上传,使用视觉反馈。

    【讨论】:

    • 我指的是照片的拍摄地点。它由用户通过文本框输入。我了解服务器因安全限制无法访问该位置
    猜你喜欢
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2015-10-13
    • 2013-07-28
    • 2021-07-27
    • 1970-01-01
    • 2012-07-05
    • 2015-08-28
    相关资源
    最近更新 更多