O que eu uso em vez de e.Values.add (... para os ListViewEditEventArgs para fazer o upload de uma imagem

StackOverflow https://stackoverflow.com/questions/250528

Pergunta

i pode fazer upload de imagens para o banco de dados usando o LINQ eo controle listview quando referancing o método e.Values ??para os ListViewInsertEventArgs, mas não existe tal método nas ListViewEditEventArgs, então o que eu posso usar para alcançar os mesmos resultados?

aqui é o meu código de inserção:

protegido ProjectPhotosList_ItemInserting void (object sender, ListViewInsertEventArgs e)

{

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

Etiqueta fileuploadlbl = (etiqueta) 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";
    }
}
Foi útil?

Solução

ok, então eu ter resolvido o problema! Eu só tinha que ir sobre ele um pouco de uma maneira diferente:

este é o código importante:

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

é apenas uma maneira simples de obter o valor primarykey da linha selecionada. Eu encontrei um post sobre upload pdf de um banco de dados e decidiu basear o resto do meu código sobre isso. Então aqui o código completo:

protegido ProjectPhotosList_ItemUpdating void (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();
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top