Trying to work with Request.Files in WebMatrix

2019-07-25 15:38发布

问题:

everyone,

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)

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];
    }

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

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

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?

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!

回答1:

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:

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)

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

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"];