locked
How to put an excel file on my asp.net site in this case ? RRS feed

  • Question

  • User-775831949 posted

    I have a ASP.Net site with standard login

    I have a local windows application which can get www.someonething.com/somepath/MyExcel.xlsx file to get the data and this link can be modified.

    How can I provide instead an alternative link using links on my asp.net site like www.myasp.net/something.xlsx that is equivalent to this above but the machine is able to connect only when this pc login to the asp.net site ?

    Thanks

    Thursday, December 20, 2018 9:29 AM

All replies

  • User-893317190 posted

    Hi hkbeer,

    You could register a httphandler to deal with request ending with .xlsx and in the handler you could ensure the request is from local pc.

    Below is my code.

     <system.webServer>
      
        <handlers>
                                     //type = "your httphandler's namespace, the name of your assembly"
         <add name="excel" verb="*" path="*.xlsx" type="MyWebFormCases.Services.HandlerXslx, MyWebFormCases"/>
    
          
        </handlers>
       
      </system.webServer>

    In your httphandler.

     public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "application/vnd.ms-excel";
                try
                {
                              //ensure the request is from local pc
                    if (HttpContext.Current.Request.UserHostAddress == "::1")
                    {
                        // write back the xlsx                    context.Response.WriteFile(HttpContext.Current.Request.MapPath(context.Request.Path));
                    }
                    else
                    {
    
                    }
                
                    
                }
                catch (Exception)
                {
    
                    throw;
                }
               
            }

    For more information , you could refer to

    https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms972953(v=msdn.10)

    Best regards,

    Ackerly Xu

    Friday, December 21, 2018 2:20 AM