Domanda

posso caricare immagini nel database usando linq e il controllo listview quando faccio riferimento al metodo e.Values ??per ListViewInsertEventArgs, ma non esiste un metodo simile in ListViewEditEventArgs, quindi cosa posso usare per ottenere gli stessi risultati?

ecco il mio codice di inserimento:

void protetto ProjectPhotosList_ItemInserting (mittente oggetto, ListViewInsertEventArgs e)

{

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

Etichetta fileuploadlbl = (Etichetta) 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";
    }
}
È stato utile?

Soluzione

ok quindi ho risolto il problema! Ho dovuto solo pensarci in un modo un po 'diverso:

questo è il codice importante:

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

è solo un modo semplice per ottenere il valore della chiave primaria della riga selezionata. Ho trovato un post sul caricamento di pdf in un database e ho deciso di basare il resto del mio codice su questo. Quindi qui il codice completo:

void protetto ProjectPhotosList_ItemUpdating (mittente oggetto, 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();
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top