1

I'm having a bit of an issue uploading files to a database. I'll post snippets of my code below.

The table i'm uploading to has the following structure:

[IDEnsayo] int NOT NULL,
[Nombre] varchar(256) NULL,
[Tipo] varchar(256) NULL,
[Longitud] int NULL,
[Contenido] image NULL

This is the class

public class Archivo
{

    public string Nombre { get; set; }
    public string Tipo { get; set; }
    public long Longitud { get; set; }
    public byte[] Contenido { get; set; }

}

I can upload the file using my application without any errors, however in the database the file stores in "Contenido" as "0x0000000000000000......". All the other information relating to the file uploads correctly.

This is my controller code:

public ActionResult Add(Ensayo Ensayo, HttpPostedFileBase uploadFile)
{
  var ensayo = new Ensayo { IDEnsayo = Ensayo.IDEnsayo};
  ensayo.Archivo.Longitud = uploadFile.ContentLength;
  ensayo.Archivo.Nombre = uploadFile.FileName;
  ensayo.Archivo.Tipo = uploadFile.ContentType;

  byte[] tempFile = new byte[uploadFile.ContentLength];
  uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);

  ensayo.Archivo.Contenido = tempFile;
  ensayo.SaveArchivo();
//

Other considerations:

  1. I use: MVC3 + SQL Server 2000.
  2. When I use the application in a testing environment (local computer + Database server) there is not problem. Files are stored correctly.
  3. When I use the application in prduction environment (web server + Database server) files are not saved correctly.
  4. IIS6 on production server

UPDATE

With IE9 works, but with Chrome does not.

This is my submit form code:

@using (Html.BeginForm("Add", "Archivos",FormMethod.Post, new { id = "attachment",      enctype = "multipart/form-data" }))
    {         
        @Html.HiddenFor(x => Model.IDEnsayo)
        <input type="file" name="uploadFile" id="uploadFile" />

    <input type="submit" value="Guardar" class="t-button"/>

    }

If anyone could offer help/advice I'd appreciate it alot.

Thanks.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
  • Is your production server the same version of sql server (2000)? – Jesse Jun 09 '12 at 01:04
  • Yes, both production and testing are connected to the same Sql Server. When I use my testing MVC3 application connected to production database (Visual Studio 2010, SQL Server 2000), no problem. Files are stored ok. But, when I use my app in production (Windows server 2003, IIS6)..files are stored incorrectly. I don't know why.. – Pablo Claus Jun 11 '12 at 11:35

2 Answers2

0

As per your last comment your file upload problem is more than likely related to IIS6 and in fact has nothing to do with SQL server. IIS6 has a silly file upload size limitation if it has not been configured correctly.

Please review: Increase file upload size limit in iis6

And more specifically for your case: http://www.banmanpro.com/support2/File_Upload_limits.asp

Community
  • 1
  • 1
Jesse
  • 8,223
  • 6
  • 49
  • 81
  • Hi Jesse. I tried it, but it didn't work. I changed the file upload size in IIS6 to 1MB and added to web.config `maxRequestLength="512000"`. The files that I uploaded have a size between 100 and 150 kB. – Pablo Claus Jun 11 '12 at 15:32
  • I tested it with IE9 and did work, but with Chrome didn't. Any suggestion? – Pablo Claus Jun 12 '12 at 17:04
  • 1
    @Pabloker what does the file upload portion of your view look like? Have you inspected the "POST" within chromes dev tools to see if it is posting the expected form values to the relevant controller? – Jesse Jun 12 '12 at 17:08
  • good tip. I compared Chrome's headers (local and server). For the same file, local transfer 700 kb but server only 17 kb. For some reason, Chrome doesn't send file's content to controller. – Pablo Claus Jun 13 '12 at 12:26
  • 1
    I found this article about a bug uploading files with Chrome when IIS is configured for Integrated Windows Authentication. I think this is my problem. http://inedo.com/support/kb/1019/workaround-for-chrome-file-uploading-bug – Pablo Claus Jun 13 '12 at 13:15
0

The problem was Chrome's version (21.0.1171.0 DEV). There is a problem with Chrome's dev versions and IIS6 using Windows Authentication and uploading files.

If you submit the form with Chrome 21 dev, submitted file content will be all zeroes.

I downloaded 19.0.1084.56 m stable version and the problem was solved.

For more information http://code.google.com/p/chromium/issues/detail?id=128574

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38