II6 File Upload in MVC storing as 0x0000…in SQL Database

末鹿安然 提交于 2019-12-24 08:46:50

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/10955051/ii6-file-upload-in-mvc-storing-as-0x0000-in-sql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!