Question

Currently, I am uploading PDF files, and images from my MVC web application to a sql server database. This works perfectly, however I now want to be able to upload ePub files.

I tried to do it with the same uploader in some vain hope that it would work, however this is the error I get:

SqlException was unhandled by the user code.

"The parameterized query '(@FileContent varbinary(max) ,@MimeType nvarchar(4000),@FileName' expects the parameter '@MimeType', which was not supplied.

Here is also the code from my controller which handles the uploads:

public ActionResult Index()
{

    foreach (string upload in Request.Files)
    {
        if (!Request.Files[upload].HasFile1()) continue;

        string mimeType = Request.Files[upload].ContentType;
        Stream fileStream = Request.Files[upload].InputStream;
        string fileName = Path.GetFileName(Request.Files[upload].FileName);
        int fileLength = Request.Files[upload].ContentLength;
        byte[] fileData = new byte[fileLength];
        fileStream.Read(fileData, 0, fileLength);

        const string connect = @"Server=localhost;Database=Images;user id=taraw; password=****;";
        using (var conn = new SqlConnection(connect))
        {
            var qry = "INSERT INTO FileStore (FileContent, MimeType, FileName) VALUES (@FileContent, @MimeType, @FileName)";
            var cmd = new SqlCommand(qry, conn);
            cmd.Parameters.AddWithValue("@FileContent", fileData);
            cmd.Parameters.AddWithValue("@MimeType", mimeType);
            cmd.Parameters.AddWithValue("@FileName", fileName);
            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

    return View();
}

I know the error is pretty self explanatory as to what the problem is, I'm just unsure how to modify the code to allow it to accept ePub File formats.

Any help would be greatly appreciated :)

Was it helpful?

Solution 2

Found out that the mimetype for an epub file is application / epub + zip. Specified this in my application rather than expecting it from the user and this seems to have done the trick. File is uploading and downloading without any issues.

Thanks to anyone who commented :)

OTHER TIPS

Add ePUB into your server's Mime type list as a known type, i guess mime-type is going null into db, that's why this error getting raised up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top