Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像从NameValueCollection上载到服务器_C#_Asp.net_Vb.net_Http Post_Namevaluecollection - Fatal编程技术网

C# 将图像从NameValueCollection上载到服务器

C# 将图像从NameValueCollection上载到服务器,c#,asp.net,vb.net,http-post,namevaluecollection,C#,Asp.net,Vb.net,Http Post,Namevaluecollection,我正在使用ASP.NET4.5和VB.net 这是我的TestPage.aspx代码 <%@ Page Language="VB" MasterPageFile="~/Master.master" AutoEventWireup="false" CodeFile="TestPage.aspx.vb" Inherits="TestPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlace

我正在使用ASP.NET4.5和VB.net

这是我的TestPage.aspx代码

    <%@ Page Language="VB" MasterPageFile="~/Master.master"  AutoEventWireup="false" CodeFile="TestPage.aspx.vb" Inherits="TestPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderMaster" runat="Server">

                        <form role="form" method="post" id="Test_form" action="TestPageLOAD.aspx"

                            <div class="form-group">
                                <input class="form-control" type="text" id="headline" name="headline" />

                            </div>

                            <div class="form-group">
                                <div class="fileUpload btn btn-default">
                                    <span>Upload article image</span>
                                    <input type='file' id="imgInp" name="imgInp" class="upload" />
                                </div>

                            </div>

                            <button  type="submit" name="submitReport" id="submitReport">SUBMIT REPORT</button>
                        </form>

</asp:Content>
现在,这段代码可以很好地填充或保存TestPageLOAD.aspx.vb上的数据,但我无法在此加载页面上载图像。有办法绕过它吗?我不想将TestPage.aspx上的控件更改为ASP.net控件,因为它需要保持纯HTML。但是现在上传图片是我的问题


VB.net或C#代码就可以了

找到了此代码,并在最后完成了工作

<form action="TestPageLOAD.aspx" method="post" enctype="multipart/form-data">
    <input type="file" name="imgInp" />
</form>

这一切都要归功于:

找到了这段代码,并在最后完成了工作

<form action="TestPageLOAD.aspx" method="post" enctype="multipart/form-data">
    <input type="file" name="imgInp" />
</form>
这一切都要归功于:

if(Request.Files["imgInp"] != null)
{
    HttpPostedFile MyFile = Request.Files["imgInp"];
    //Setting location to upload files
    string TargetLocation = Server.MapPath("~/pics/");
    try
    {
        if (MyFile.ContentLength > 0)
            {
                //Determining file name. You can format it as you wish.
                string FileName = MyFile.FileName;
                //Determining file size.
                int FileSize = MyFile.ContentLength;
                //Creating a byte array corresponding to file size.
                byte[] FileByteArray = new byte[FileSize];
                //Posted file is being pushed into byte array.
                MyFile.InputStream.Read(FileByteArray, 0, FileSize);
                //Uploading properly formatted file to server.
                MyFile.SaveAs(TargetLocation + FileName);
            }
        }
     catch(Exception BlueScreen)
     {
         //Handle errors
     }
}