如何从 InputStremed 文件获取更多信息?

发布于 2024-12-02 13:31:08 字数 1822 浏览 0 评论 0原文

如果我使用 InputStream 接收文件,例如

HttpContext.Current.Request.InputStream

如何获取有关该文件的更多信息?

我可以轻松地将流转换为物理文件,但例如,我如何知道正在使用的文件扩展名

string fileIn = @"C:\Temp\inputStreamedFile.xxx"; // What extension?

using (FileStream fs = System.IO.File.Create(fileIn))
{
    Stream f = HttpContext.Current.Request.InputStream;
    byte[] bytes = new byte[f.Length];
    f.Read(bytes, 0, (int)f.Length);
    fs.Write(bytes, 0, bytes.Length);
}

这背后的想法是因为使用 HttpPostedFileBase 我总是得到 null:

public ContentResult Send(HttpPostedFileBase fileToUpload, string email)
{
    // Get file stream and save it

    // Get File in stream
    string fileIn = Path.Combine(uploadsPath, uniqueIdentifier),
           fileOut = Path.Combine(convertedPath, uniqueIdentifier + ".pdf");

    // Verify that the user selected a file
    if (fileToUpload != null && fileToUpload.ContentLength > 0)
    {
        // extract only the fielname
        string fileExtension = Path.GetExtension(fileToUpload.FileName);
        fileIn = String.Concat(fileIn, fileExtension);

        fileToUpload.SaveAs(fileIn);
    }

    // TODO: Add Convert File to Batch

    return Content("File queued for process with id: " + uniqueIdentifier);
}

这就是我从命令行发送的内容:

$ curl --form email='[email protected]' --form fileToUpload='C:\temp\MyWord.docx' http://localhost:64705/send/
File queued for process with id: 1d777cc7-7c08-460c-8412-ddab72408123

变量 email 已正确填写,但是 < code>fileToUpload 始终为空。

PS 如果我使用表单上传相同的数据,则不会发生这种情况

If I'm using the InputStream to receive a file, like

HttpContext.Current.Request.InputStream

How can get more information about the file?

I can easily convert a Stream into a phisical File, but for example, how would I know the file extension in use?

string fileIn = @"C:\Temp\inputStreamedFile.xxx"; // What extension?

using (FileStream fs = System.IO.File.Create(fileIn))
{
    Stream f = HttpContext.Current.Request.InputStream;
    byte[] bytes = new byte[f.Length];
    f.Read(bytes, 0, (int)f.Length);
    fs.Write(bytes, 0, bytes.Length);
}

The idea behind this is because using HttpPostedFileBase I always get null:

public ContentResult Send(HttpPostedFileBase fileToUpload, string email)
{
    // Get file stream and save it

    // Get File in stream
    string fileIn = Path.Combine(uploadsPath, uniqueIdentifier),
           fileOut = Path.Combine(convertedPath, uniqueIdentifier + ".pdf");

    // Verify that the user selected a file
    if (fileToUpload != null && fileToUpload.ContentLength > 0)
    {
        // extract only the fielname
        string fileExtension = Path.GetExtension(fileToUpload.FileName);
        fileIn = String.Concat(fileIn, fileExtension);

        fileToUpload.SaveAs(fileIn);
    }

    // TODO: Add Convert File to Batch

    return Content("File queued for process with id: " + uniqueIdentifier);
}

and this is what I'm sending from the command line:

$ curl --form email='[email protected]' --form fileToUpload='C:\temp\MyWord.docx' http://localhost:64705/send/
File queued for process with id: 1d777cc7-7c08-460c-8412-ddab72408123

the variable email is filled up correctly, but fileToUpload is always null.

P.S. This does not happen if I use a form to upload the same data.

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

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

发布评论

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

评论(3

明媚如初 2024-12-09 13:31:08

如果这没有帮助,我很抱歉,但是为什么使用 InputStream 来获取上传的文件?

这就是我通常做的事情:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase[] files) {
    String physicalPath = "c:\\whatever";
    foreach (var file in files) {
        String extension = Path.GetExtension(file.FileName);
        file.SaveAs(physicalPath + "\\" + file.FileName);
    }
    return View();
}

I'm sorry if this doesn't help, but why use InputStream to get uploaded file(s) ?

This is what I usually do:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase[] files) {
    String physicalPath = "c:\\whatever";
    foreach (var file in files) {
        String extension = Path.GetExtension(file.FileName);
        file.SaveAs(physicalPath + "\\" + file.FileName);
    }
    return View();
}
丑疤怪 2024-12-09 13:31:08

我发现的唯一问题是使用curl...我忘记了@ 符号,该符号提到上传的表单将被编码为multipart/form-data

使用HttpPostedFileBase的正确curl命令是:

$ curl --form email='[email protected]' 
       --form fileToUpload=@'C:\temp\MyWord.docx' 
       http://localhost:64705/send/

The only problem I found was using curl... I was forgetting the @ sign that mention that the uploaded form would be encoded as multipart/form-data.

The correct curl command to use HttpPostedFileBase would be:

$ curl --form email='[email protected]' 
       --form fileToUpload=@'C:\temp\MyWord.docx' 
       http://localhost:64705/send/
纵情客 2024-12-09 13:31:08

您可以从获取已发布文件的信息。但实际上它在 asp.net mvc 中使用了一种不同的方式上传文件 在这里查看

You can get the info about posted file from <input type="file" />. But actually it's used a bit different way to upload files in asp.net mvc check out here

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