0

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

http://www.mikesdotnetting.com/Article/148/Save-And-Retrieve-Files-From-a-Sql-Server-CE-Database-with-WebMatrix

Daniel Lavoie
  • 109
  • 16
  • Why don't you handle this in the controller? I'm not understand where you get to this code. If you use a ViewModel to create the architecture for the form fields you will need you can follow this example: http://stackoverflow.com/questions/9909521/validate-file-size-before-upload/16086008#16086008 It will allow you to upload a file much easier. Everything you are trying to do in your code should be done in a controller, not a view. – Termato Oct 14 '14 at 19:30
  • in your view use `form` having enctype = `multipart` , explained clearly here - http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0 – Arindam Nayak Oct 14 '14 at 19:35
  • Arindam Nayak you were right. Can you post it as answer so I can give you credit? – Daniel Lavoie Oct 15 '14 at 20:15

1 Answers1

0

Finally, the main reason why it wasn't working was because of the OS of the phone my users had. Old OS 5 on Blackberry was known to have problems performing the upload of image on their browser =(

Thank you

Daniel Lavoie
  • 109
  • 16