简体   繁体   中英

Send File to Asp.net

I am trying to test a possible application. It is an ASP.net site that will receive xml files in its default page. The client will be an application program that will generate a HTTP Post containing the file.

There is no UI involvement.

I made the prototype Asp.Net Web Application (Framework 4.5.2) and a Winform application to exercise it.

I have tried all manner of HTTP posting code in the Winform application but although the WebApp default.aspx gets hit I cannot get to the payload.

This includes:

  • the test app reading in the file and Posting the string. The StreamReader in the Webpage produces an empty string when it reads Request.InputStream.

  • a WebClient.UpLoadFile attempt. The Request.Files collection is empty.

Using Postman and a POST with parameters the StreamReader scenario works. ie a body of file:"opennode This is a node closenode" produces an xml string output.

I should mention I am using the IDE to host the Webpage at present in case this has any effect on behavior.

Given the requirement for an automatic transfer between the app and the site what is the correct way to tackle this?

I would normally put up a web service but the Website consumer thinks this is overkill and just wants to hit a URL with the file.

Application code for WebClient Attempt follows.

 private void OpenFile(string _file)
    {

        // WEBCLIENT TRY
        using (WebClient client = new WebClient())
        {
            client.UploadFile("http://localhost:21726/Default.aspx", _file);
        }


    }

ASP.NET CODE 'Web Client Receive' Follows

 private void ProcessData(HttpRequest request)
    {
        try
        {

            // WEB CLIENT RECEIVE ATTEMPT

                foreach (string f in Request.Files.AllKeys)
                {
                    HttpPostedFile file = Request.Files[f];
                    file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
                }


        catch (Exception ex)
        {

            string message = ex.Message;
            throw;
        }
    }

Web Page Altered to Sample Code from Microsoft Webclient.UploadFile example(below). No Change in behavior. So it must be the WinApp Tester.

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e) {

    foreach(string f in Request.Files.AllKeys) {
        HttpPostedFile file = Request.Files[f];
        file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
    }   
}

</Script>
<html>
<head></head>
<body>
<p> Upload complete.  </p>
</body>
</html>

Problem was two fold: The webApp appears OK under the IDE giving the symptom of no files in the RequestFiles container out lined above. Once I published to the local IIS It started handing back error 500 to the WinApp test client. This we traced down to directory access rights (funny that). We were blithely trying to write to a location outside of the site.
So added an Upload subdirectory to the site and set full permissions. Then altered the file writing code to :

foreach(string f in Request.Files.AllKeys)
    {
        //File.Create(@"C:\test1.txt");
        HttpPostedFile file = Request.Files[f];
        file.SaveAs(Server.MapPath("~/Upload/") + file.FileName);
    }

All is now working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM