尝试使用WebMatrix中的Request.Files [英] Trying to work with Request.Files in WebMatrix

查看:78
本文介绍了尝试使用WebMatrix中的Request.Files的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有人

我正在尝试将图像上传到我的网站,并且已经成功地使它可以工作,但是,我需要将功能扩展到一个简单图像之外.我看到的示例使用WebMatrix File Helper(File Helper?是吗?哦,这是一种自动绘制input = type"file"字段所需的html的帮助程序).我采用以下形式的代码行:

I am trying to work with uploading images to my site, and I have successfully gotten it to work, however, I need to extend the functionality beyond that of one simple image. The examples I have seen use the WebMatrix File Helper (File Helper? Is that right? Oh well, it's a helper of some kind that auto plots the html necessary for the input=type"file" field). The line of code I have in the form:

@FileUpload.GetHtml(initialNumberOfFiles:1, allowMoreFilesToBeAdded:false, includeFormTag:false)

我在(IsPost)中拥有的代码行:

The line of code I have in (IsPost):

var UploadedPicture = Request.Files[0];
    if(Path.GetFileName(UploadedPicture.FileName) != String.Empty)
    {
        var ContentType = UploadedPicture.ContentType;
        var ContentLength = UploadedPicture.ContentLength;
        var InputStream = UploadedPicture.InputStream;
        Session["gMugshot"] = new byte[ContentLength];
        InputStream.Read((byte[])Session["gMugshot"], 0, ContentLength);
    }
    else
    {
        Session["gMugshot"] = new byte[0];
    }

(IsPost)中的更多代码将其作为二进制数据存储在数据库中之后,我可以从中获取图像(我不希望将实际图像文件保存在服务器上的文件夹中并使用GUID等.二进制数据很好,我想占用的空间要少得多.

More code in the (IsPost) after this stores it in the database as binary data, and I can get the image back on the page from that (I have no desire to save the actual image files in a folder on the server and use GUID, etc. etc. Binary data is fine, and I imagine takes up a lot less space).

我将其设置为通过使用jQuery来读取可手动滚动的图片以读取手动创建的按钮的单击,然后隐藏和取消隐藏包含C#渲染的图像的div(从读取数据库中获取它们).抱歉,如果有点TMI,请尝试做一个更彻底的讲,但是要提一个问题:我对文件上传的了解还不够,还不知道如何很好地处理上传的数据.我尝试研究此信息,但没有找到任何与我相关的信息(实际上,我根本没有在输入type ="file"或FileUpload方法上找到很多有用的信息).

I have it set up to click-ably scroll through pictures by using jQuery to read the clicks of manually created buttons and subsequently hide and unhide the divs that contain the images rendered by C# (which gets them from reading the database). Sorry if that's a little TMI, just trying to be thorough, but to refine the question: I don't know enough about file uploading to know how to work with the uploaded data that well yet. I tried researching this information, but I didn't find any information that seemed pertinent to me (actually, I didn't find much useful information on input type="file", or the FileUpload method, at all, really).

使用输入type ="file" id ="pic1id"更好吗?我可以使用诸如Request.Files ["pic1id"]之类的东西来从输入元素的ID中获取文件吗?还是该程序只是获取所有上载的文件,将它们放在后勤组中,等待索引被调用,例如:"Request.Files [0]".如果是这样,索引将以什么顺序输入?在开始使用数据之前,是否需要使用Request.Files.Count来测试已经上传了多少?

Would it be better to use input type="file" id="pic1id"? Is there something that I can use such as Request.Files["pic1id"] that could get the file from the id of the input element? Or does the program simply take all uploaded files, stick them in a logistical group somewhere waiting to be called by index like this: "Request.Files[0]". If so, what order does the index get put in? Do I need to use Request.Files.Count to test how many have been uploaded before I begin working with the data?

请注意,我需要单独的输入type ="file"字段(无论是否由助手绘制).我不想在一个输入中接受多个文件(主要是由于缺乏知识,例如,恐怕我不知道如何处理数据).到目前为止,我们的计划是,单独的输入type ="file"字段将位于在滚动图片时被隐藏/隐藏的div内,因此每个图片(空间)将具有自己的输入type ="file"字段. div的隐藏和取消隐藏(显示一张图片),从数据库中存储和接收二进制数据,以及单击图片占位符都非常有用.我几乎只需要知道一次一次处理多张上传的图片,以将其存储在其各自的数据库图像"字段中.

Please note that I want separate input type="file" fields (whether plotted by the helper or not). I do not want to accept multiple files in one input (mainly because of a lack of knowledge, e.g., I am afraid I won't know how to work with the data). So far, the plan is that the separate input type="file" fields will be within the divs that get hidden/unhidden upon scrolling through pictures, so each picture (space) will have its own input type="file" field. The hiding and unhiding of divs, (the one) picture being displayed, storing and receiving binary data from the database, and clicking through the picture placeholders all function great. Pretty much I just need to know how to work with more than one uploaded picture at a time for storage in their individual database "image" fields.

任何需要使用的语法示例将不胜感激.

Any examples of the syntax I need to use will be much appreciated.

很抱歉有这么多个问题,但是我根本找不到很多有用的信息.

Sorry so many questions, but I just couldn't find much useful information on this at all.

感谢任何尝试提供帮助的人!

Thanks to any who try to help!

推荐答案

好,为了解决这个问题,我不得不进行测试,测试和测试,直到最终对我有用.这是我所做的:

Okay, in order to solve this, I had to test and test and test, until something finally worked for me. Here's what I did:

首先,我放弃使用绘制HTML的帮助程序部分,即我将其取出:

First, I abandoned my use of the part of the helper that plotted the html, that is I took out:

@FileUpload.GetHtml(initialNumberOfFiles:1, allowMoreFilesToBeAdded:false, includeFormTag:false)

并添加具有特定ID(例如id ="pic1")的常规输入type ="file".

And added a regular input type="file" with a certain id, such as id="pic1".

接下来,我能够基于id获取单个文件,这实际上是我需要知道的主要方法,而且确实如此简单:

Next I was able to get the individual file post based on id, which was really the main thing I needed to know how to do, and it really was as simple as this:

Request.Files["pic1"];

这篇关于尝试使用WebMatrix中的Request.Files的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆