我应该使用什么代替 e.Values.add(... 用于 ListViewEditEventArgs 上传图像

发布于 2024-07-07 23:44:10 字数 1238 浏览 13 评论 0原文

当引用 ListViewInsertEventArgs 的 e.Values 方法时,我可以使用 linq 和 listview 控件将图像上传到数据库,但 ListViewEditEventArgs 中没有这样的方法,那么我可以使用什么来实现相同的结果?

这是我的插入代码:

protected void ProjectPhotosList_ItemInserting(object sender, ListViewInsertEventArgs e)

{

FileUpload uplImage = (FileUpload)ProjectPhotosList.InsertItem.FindControl("uplImage");

标签 fileuploadlbl = (Label)ProjectPhotosList.InsertItem.FindControl("fileuploadlbl");

    byte[] img = null;
    if (uplImage.HasFile || !uplImage.FileName.ToLower().EndsWith(".jpg"))
    {
        try
        {
            img = new byte[uplImage.PostedFile.ContentLength];
            uplImage.PostedFile.InputStream.Read(img, 0, img.Length);
        }
        catch
        {
            fileuploadlbl.Text = "unable to upload " + uplImage.FileName.ToString();
        }
    }
    if (img == null)
    {
        e.Cancel = true;
        fileuploadlbl.Text = "Please choose a file to upload";
    }

    try
    {
        e.Values.Add("ProjectPhoto", new System.Data.Linq.Binary(img));
        fileuploadlbl.Text = "File Upload Successful";
    }
    catch
    {
        fileuploadlbl.Text = "File Upload Failed, please try again";
    }
}

i can upload images to the database using linq and the listview control when referancing the e.Values method for the ListViewInsertEventArgs, but there is no such method in the ListViewEditEventArgs, so what can i use to achieve the same results?

here is my inserting code:

protected void ProjectPhotosList_ItemInserting(object sender, ListViewInsertEventArgs e)

{

FileUpload uplImage = (FileUpload)ProjectPhotosList.InsertItem.FindControl("uplImage");

Label fileuploadlbl = (Label)ProjectPhotosList.InsertItem.FindControl("fileuploadlbl");

    byte[] img = null;
    if (uplImage.HasFile || !uplImage.FileName.ToLower().EndsWith(".jpg"))
    {
        try
        {
            img = new byte[uplImage.PostedFile.ContentLength];
            uplImage.PostedFile.InputStream.Read(img, 0, img.Length);
        }
        catch
        {
            fileuploadlbl.Text = "unable to upload " + uplImage.FileName.ToString();
        }
    }
    if (img == null)
    {
        e.Cancel = true;
        fileuploadlbl.Text = "Please choose a file to upload";
    }

    try
    {
        e.Values.Add("ProjectPhoto", new System.Data.Linq.Binary(img));
        fileuploadlbl.Text = "File Upload Successful";
    }
    catch
    {
        fileuploadlbl.Text = "File Upload Failed, please try again";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烈酒灼喉 2024-07-14 23:44:10

好的,我已经解决了这个问题! 我只需要采取一些不同的方式:

这是重要的代码:

int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());

它只是获取所选行的主键值的简单方法。
我发现了一篇关于将 pdf 上传到数据库的帖子,并决定将我的其余代码基于该帖子。 所以这里是完整的代码:

protected void ProjectPhotosList_ItemUpdating(object sender, ListViewUpdateEventArgs e)

{

FileUpload myFile = (FileUpload)ProjectPhotosList.EditItem.FindControl("uploadImage");

    TextBox myCaption = (TextBox)ProjectPhotosList.EditItem.FindControl("ProjectPhotoCaptionTextBox");

    int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());

    if (myFile.HasFile)
    {

        //Get the posted file
        Stream fileDataStream = myFile.PostedFile.InputStream;

        //Get length of file
        int fileLength = myFile.PostedFile.ContentLength;

        //Create a byte array with file length
        byte[] fileData = new byte[fileLength];

        //Read the stream into the byte array
        fileDataStream.Read(fileData, 0, fileLength);

        //get the file type
        string fileType = myFile.PostedFile.ContentType;

        //Open Connection
        PHJamesDataContext db = new PHJamesDataContext();
        //Find the Right Row
        PHJProjectPhoto Newphoto = (from p in db.PHJProjectPhotos
                                    where p.ProjectPhotoId == mykey
                                    select p).Single<PHJProjectPhoto>();


        Newphoto.ProjectPhoto = fileData;

        db.SubmitChanges();
    }

ok so i have solved the issue! I just had to go about it a bit of a different way:

this is the important code:

int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());

its just a simple way to get the primarykey value of the selected row.
I found a post about uploading pdf's to a database and decided to base the rest of my code on that. So here the full code:

protected void ProjectPhotosList_ItemUpdating(object sender, ListViewUpdateEventArgs e)

{

FileUpload myFile = (FileUpload)ProjectPhotosList.EditItem.FindControl("uploadImage");

    TextBox myCaption = (TextBox)ProjectPhotosList.EditItem.FindControl("ProjectPhotoCaptionTextBox");

    int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString());

    if (myFile.HasFile)
    {

        //Get the posted file
        Stream fileDataStream = myFile.PostedFile.InputStream;

        //Get length of file
        int fileLength = myFile.PostedFile.ContentLength;

        //Create a byte array with file length
        byte[] fileData = new byte[fileLength];

        //Read the stream into the byte array
        fileDataStream.Read(fileData, 0, fileLength);

        //get the file type
        string fileType = myFile.PostedFile.ContentType;

        //Open Connection
        PHJamesDataContext db = new PHJamesDataContext();
        //Find the Right Row
        PHJProjectPhoto Newphoto = (from p in db.PHJProjectPhotos
                                    where p.ProjectPhotoId == mykey
                                    select p).Single<PHJProjectPhoto>();


        Newphoto.ProjectPhoto = fileData;

        db.SubmitChanges();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文