¿Qué uso en lugar de e.Values.add (& # 8230; para que ListViewEditEventArgs cargue una imagen?)
-
05-07-2019 - |
Pregunta
puedo cargar imágenes a la base de datos usando linq y el control listview cuando se hace referencia al método e.Values ??para ListViewInsertEventArgs, pero no hay tal método en ListViewEditEventArgs, así que, ¿qué puedo usar para lograr los mismos resultados?
aquí está mi código de inserción:
void protegido ProjectPhotosList_ItemInserting (objeto remitente, 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";
}
}
Solución
ok, así que he resuelto el problema! Solo tuve que hacerlo de una manera diferente:
este es el código importante:
int mykey = int.Parse (ProjectPhotosList.DataKeys [e.ItemIndex] .Value.ToString ());
es solo una forma sencilla de obtener el valor de clave principal de la fila seleccionada. Encontré una publicación sobre la carga de archivos PDF a una base de datos y decidí basar el resto de mi código en eso. Así que aquí el código completo:
void protegido ProjectPhotosList_ItemUpdating (objeto remitente, 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();
}