从 ASP.NET MVC 应用程序上传 ePub 文件

发布于 2024-09-29 21:30:45 字数 1568 浏览 5 评论 0原文

目前,我正在将 PDF 文件和图像从 MVC Web 应用程序上传到 SQL Server 数据库。这工作得很好,但我现在希望能够上传 ePub 文件。

我尝试使用同一个上传器来完成此操作,徒劳地希望它能起作用,但是这是我得到的错误:

用户未处理 SqlException 代码。

“参数化查询 '(@FileContent varbinary(最大) ,@MimeType nvarchar(4000),@文件名' 需要参数“@MimeType”, 未提供。

这也是我的控制器处理上传的代码:

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();
}

我知道这个错误对于问题是什么来说是非常不言自明的,我只是不确定如何修改代码以允许它接受 ePub 文件格式。

任何帮助将不胜感激:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月寒剑心 2024-10-06 21:30:45

将 ePUB 作为已知类型添加到服务器的 Mime 类型列表中,我猜 mime-type 将在 db 中变为 null,这就是引发此错误的原因。

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.

ぶ宁プ宁ぶ 2024-10-06 21:30:45

发现 epub 文件的 mimetype 是 application/epub + zip。在我的应用程序中指定了这一点,而不是期望用户这样做,这似乎已经成功了。文件上传和下载没有任何问题。

感谢任何发表评论的人:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文