Friday 30 December 2016

.asp upload to server by iis

client sends file


service receives file



create .asp web project

//default.asp

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="game_mine._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
            <ContentTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="UploadButton" runat="server" Text="Upload Selected File" OnClick="UploadButton_Click" />
                <asp:Label ID="UploadDetails" runat="server" Text=""></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="UploadButton" />
            </Triggers>
        </asp:UpdatePanel>

    </form>
</body>
</html>

------------------------------------------------------------------------------
//default.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace game_mine
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile == false)
            {
                UploadDetails.Text = "Please first select a file to upload...";
            }
            else
            {
                string FileName = FileUpload1.FileName;
                UploadDetails.Text = string.Format(
                        @"Uploaded file: {0}<br />
              File size (in bytes): {1:N0}<br />
              Content-type: {2}",
                          FileName,
                          FileUpload1.FileBytes.Length,
                          FileUpload1.PostedFile.ContentType);

                // Save the file
               // for visual studio debug upload folder address is the project address
               //C:\Users\xxxxxx\Documents\Visual Studio 2015\Projects\game_mine\game_mine

                string filePath = Server.MapPath("~/upload/" + FileUpload1.FileName);
                FileUpload1.SaveAs(filePath);
            }
        }
    }
}

add upload folder in project directory for debugging


//web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>

    <!--maxRequestLength for file transfer 1MB = 1024  5MB-->
    <httpRuntime targetFramework="4.5.2" maxRequestLength="5120"/>
 
    <customErrors mode="Off"></customErrors>
 
  </system.web>

  <system.webServer>
    
    <security>
      <requestFiltering>
        
        <!--maxAllowedContentLength (maximum number of bytes in an IIS Request) 1KB = 1024  10MB-->
        <requestLimits maxAllowedContentLength="10485760" />
        
      </requestFiltering>           
    </security>
  </system.webServer>

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>


open iis->right click on website->explore

make a new upload folder which is at same iis map path(~/upload) for receiving client files

publish project

try to send file larger than -maxRequestLength that server is set to receive (5Mb) on client side


reference:

http://stackoverflow.com/questions/9682737/fileupload-hasfile-is-always-false
http://asp.net-tutorials.com/controls/file-upload-control/
http://stackoverflow.com/questions/1989334/how-do-i-enable-upload-of-large-files-in-classic-asp-on-iis-7
http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded

No comments:

Post a Comment