Ok nu tror jeg da at jeg har forstået dig rigtigt, ved at smide mine sql statements ind efter
System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
men det har jeg også gjort nu, men det virker stadig ikke.
Nu ser min kode sådan ud:
// Initialize variables
string sSavePath;
string sThumbExtension;
// Set constant values
sSavePath = "images/";
sThumbExtension = "_thumb";
if (FileUpload.PostedFile != null)
{
// Check file size (mustn’t be 0)
HttpPostedFile myFile = FileUpload.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
{
lblOutput.Text = "Du skal uploade en fil.";
return;
}
if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg")
{
lblOutput.Text = "det skal være en jpg fil";
return;
}
// Read file into a data stream
byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
// Make sure a duplicate file doesn’t exist. If it does, keep on appending an
// incremental numeric until it is unique
string sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + ".jpg";
}
// Save the stream to disk
System.IO.FileStream newFile
= new System.IO.FileStream(Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create);
newFile.Write(myData, 0, myData.Length);
newFile.Close();
SqlCommand objcmd = new SqlCommand();
objcmd.CommandType = CommandType.Text;
objcmd.Connection = objconn;
objcmd.CommandText = "UPDATE PROGRAM SET titel = @titel, img_url = @img_url WHERE id = @id";
objcmd.Parameters.AddWithValue("@titel", (titel.Value));
objcmd.Parameters.AddWithValue("@img_url", FileUpload.Value);
objcmd.Parameters.AddWithValue("id", Request.QueryString["id"]);
objconn.Open();
objcmd.ExecuteNonQuery();
objconn.Close();
// If jpg file is a jpeg, create a thumbnail filename that is unique.
file_append = 0;
string sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ sThumbExtension + ".jpg";
while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + sThumbExtension + ".jpg";
}
}
}
}
Var det ikke det du mente?