Asp.net mvc 3 如何从输入的文件中获取更多信息?
如果我使用Asp.net mvc 3 如何从输入的文件中获取更多信息?,asp.net-mvc-3,file-upload,inputstream,Asp.net Mvc 3,File Upload,Inputstream,如果我使用InputStream接收文件,如 HttpContext.Current.Request.InputStream 如何获取有关该文件的更多信息 我可以轻松地将流转换为物理文件,但例如,如何知道正在使用的文件扩展名 string fileIn = @"C:\Temp\inputStreamedFile.xxx"; // What extension? using (FileStream fs = System.IO.File.Create(fileIn)) { Stream
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
我总是得到空值:
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='mail@domain.com' --form fileToUpload='C:\temp\MyWord.docx' http://localhost:64705/send/
File queued for process with id: 1d777cc7-7c08-460c-8412-ddab72408123
变量email
填写正确,但fileToUpload
始终为空
请注意,如果我使用表单上载相同的数据,则不会发生这种情况。如果这没有帮助,我很抱歉,但是为什么要使用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();
}
您可以从
获取有关已发布文件的信息。但实际上,在asp.net mvc中上传文件的方式有点不同,我发现的唯一问题是使用curl。。。我忘记了@
符号,该符号提到上传的表单将被编码为多部分/表单数据
使用HttpPostedFileBase
的正确curl命令是:
$ curl --form email='mail@domain.com'
--form fileToUpload=@'C:\temp\MyWord.docx'
http://localhost:64705/send/
因为我想在cURL、PowerShell和其他脚本语言中使用它,所以我不需要文件上传页面。例如,要用作calDav解析器,所有电子邮件客户端都将
.ics
文件作为输入流发送,尽管我使用的库可以直接从流中读取,但现在我正在尝试制作一个简单的PDF转换器,以便在命令行中使用。@balexandre,只要您的客户端支持多部分/表单数据
(受人尊敬的客户端,如cURL)您可以使用HttpPostedFileBase
而不会出现任何问题。我很确定,如果您将一些数据直接流式传输到服务器上,任何地方都找不到文件名。作为一个流,您不能期望文件名,它只是二进制数据,不一定是文件。@Darin usingcURL——form email="mail@domain.com“--form fileToUpload=“c:\temp\myWord.docx”http://localhost:1112/send/“
我总是在我的公共内容结果发送(HttpPostedFileBase fileToUpload,string email)
中得到null
,但在电子邮件中没有,这就是为什么我想使用输入流
,因为我在那里得到文件(好吧,至少ÌnputStream`包含一些东西…。我知道如何在MVC中上传文件,我只想使用InputStream。