【问题标题】:Uploading Image to Server from NameValueCollection从 NameValueCollection 上传图片到服务器
【发布时间】:2015-06-02 18:09:40
【问题描述】:

我正在使用带有 VB.net 的 ASP.net 4.5

这是我的 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>

这是我在 Page_Load 事件中的 TestPageLOAD.aspx.vb 代码

 Dim nvc As NameValueCollection = Request.Form()

    lblHeadline.text = nvc("headline")

    Dim MyFileToUpload As String
    MyFileToUpload = nvc("imgInp")

    Dim FolderPath As String
    Dim FolderPathDOC As String

    FolderPath = "~/pics/"

    If MyFileToUpload.HasFile Then

        Dim strFileName As String
        Dim Extention As String = System.IO.Path.GetFileName(MyFileToUpload.PostedFile.FileName)
        Dim NoExtention As String = System.IO.Path.GetFileNameWithoutExtension(MyFileToUpload.PostedFile.FileName)

        strFileName = NoExtention + "_logo" + Extention.Substring(Extention.LastIndexOf("."))

        Dim filePath As [String] = Server.MapPath(FolderPath & strFileName)

        MyFileToUpload.SaveAs(filePath)

    End If

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

VB.net 或 C# 代码都可以

【问题讨论】:

    标签: c# asp.net vb.net http-post namevaluecollection


    【解决方案1】:

    找到了这段代码,最后完成了这项工作。

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

    TestPageLOAD.aspx.cs 上的 Page_Load 事件

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

    感谢:Uploading Files in ASP.net without using the FileUpload server control

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 2012-02-19
      • 2011-06-01
      相关资源
      最近更新 更多