如何从 InputStremed 文件获取更多信息?
如果我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这没有帮助,我很抱歉,但是为什么使用 InputStream 来获取上传的文件?
这就是我通常做的事情:
I'm sorry if this doesn't help, but why use InputStream to get uploaded file(s) ?
This is what I usually do:
我发现的唯一问题是使用curl...我忘记了
@
符号,该符号提到上传的表单将被编码为multipart/form-data
。使用
HttpPostedFileBase
的正确curl命令是:The only problem I found was using curl... I was forgetting the
@
sign that mention that the uploaded form would be encoded asmultipart/form-data
.The correct curl command to use
HttpPostedFileBase
would be:您可以从
获取已发布文件的信息。但实际上它在 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