I got this "uploading file" code here taken from another site (because I'm too stupid to create one by myself) and I tryed to modify it by adding form entries.
@{
int id = 0;
var fileName = "";
var fileMime = "";
if (IsPost) {
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
if(fileName != String.Empty)
{
fileMime = uploadedFile.ContentType;
var fileStream = uploadedFile.InputStream;
var fileLength = uploadedFile.ContentLength;
byte[] fileContent = new byte[fileLength];
fileStream.Read(fileContent, 0, fileLength);
var No = Request.Form["No"];
var Rue = Request.Form["Rue"];
var CP = Request.Form["CP"];
var Date = DateTime.Now.ToString("yyyy-MM-dd");
var Etat = "En attente";
var Commentaire = Request.Form["Commentaire"];
var db = Database.Open("Empl2");
var sql = "INSERT INTO Files (FileName, FileContent, MimeType, No, Rue, CP, Date, Commentaire, Etat) VALUES (@0,@1,@2,@3,@4,@5,@6,@7,@8)";
db.Execute(sql, fileName, fileContent, fileMime, No, Rue, CP, Date, Commentaire, Etat);
id = (int)db.GetLastInsertId();
}
}
}
I got this message when I'm trying to use it
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
And here is the line:
var uploadedFile = Request.Files[0];
I tried a lot of things but so far nothing works.
There is also this code (uploadfile helper) in the html page...
@FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag:true,
uploadText:"Envoyer")
@if (IsPost && fileName != String.Empty) {
<span>Déclaration émise!</span><br/>
if(fileMime.StartsWith("image")){
<img src="Download.cshtml?Id=@id" alt="" />
}
else {
<a href="Download.cshtml?Id=@id">Cliquez ici</a>
}
}
Here is the site where I got the code... As I said, the picture alone is uploading with success... I just want to add a few informations with the picture