C# and VC6 Web Services File Upload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chizl

    C# and VC6 Web Services File Upload

    I'm cross posting, sorry, but this pertains to both sides..

    I have a Web Services written in C# and a client written in VC6. I've
    never uploaded files through web services so forgive my ignorance in this
    matter, but I'm trying to understand how I can do this from the C++ client.

    The C# code works for HTTP Form Posts, but not really what I'm looking for..
    This should be for applications to upload files too.. The code is as
    follows:
    -----------------------------------------------------------------
    [WebMethod]
    public bool UploadFileColle ction()
    {
    try
    {
    //HTTP Context to get access to the submitted data
    HttpContext postedContext = HttpContext.Cur rent;

    //File Collection that was submitted with posted data
    HttpFileCollect ion Files = postedContext.R equest.Files;

    //Make sure a file was posted
    //This works for HTTP Form Posts, need to understand how to make this
    work for C++ posts
    string fileName = (string)postedC ontext.Request. Form["fileName"];

    if (Files.Count == 1 && Files[0].ContentLength 1 && fileName != null
    && fileName != "")
    {
    //The byte array we'll use to write the file with
    byte[] binaryWriteArra y = new
    byte[Files[0].InputStream.Le ngth];
    //Read in the file from the InputStream
    Files[0].InputStream.Re ad(binaryWriteA rray, 0,
    (int)Files[0].InputStream.Le ngth);
    //Open the file stream
    FileStream objfilestream = new FileStream("c:\ \" + fileName,
    FileMode.Create , FileAccess.Read Write);
    //Write the file and close it
    objfilestream.W rite(binaryWrit eArray, 0,
    binaryWriteArra y.Length);
    objfilestream.C lose();
    return true;
    }
    else
    {
    return false;
    }
    }
    catch (Exception ex1)
    {
    throw new Exception("Prob lem uploading file: " + ex1.Message);
    }
    }

    -----------------------------------------------------------------


    I have VC6 client that is to upload a file to the C# Web Services Interface
    using WinHttp..
    WinHttpOpen
    WinHttpConnect
    WinHttpOpenRequ est
    WinHttpSetOptio n
    WinHttpAddReque stHeaders
    WinHttpWriteDat a //header
    WinHttpWriteDat a //binary file in data chunks
    WinHttpWriteDat a //footer
    WinHttpReceiveR esponse
    WinHttpQueryHea ders
    WinHttpQueryDat aAvailable
    WinHttpReadData
    WinHttpCloseHan dle

    I've never written anything in C++ to talk to Web Services before and I know
    this code work for file uploads to IIS via ASPUpload object on the server.
    I may be way off, but my searching is leading me no where.


  • Peter Morris

    #2
    Re: C# and VC6 Web Services File Upload

    Can't you accept two parameters like so?


    public void UploadFile(stri ng fileName, byte[] fileData)


    Or is that bad practise for some reason I am unaware of?


    Pete


    Comment

    • Chizl

      #3
      Re: C# and VC6 Web Services File Upload

      Problem with two params, is now, I step out of the application post binary
      to a website as we do today using ASPUpload object on the server. I really
      don't want to change our client process, since it's rock solid, but was
      willing to tweak it if necessary. However Marc Gravell's suggestion on
      the Web Services side I'm going to try.. The web services part is all new
      and I can handle it most anyway I want.

      "Peter Morris" <peter[dot]morris(at)capab leobjects.comwr ote in message
      news:umVjUxAlIH A.3636@TK2MSFTN GP02.phx.gbl...
      Can't you accept two parameters like so?
      >
      >
      public void UploadFile(stri ng fileName, byte[] fileData)
      >
      >
      Or is that bad practise for some reason I am unaware of?
      >
      >
      Pete
      >

      Comment

      • Marc Gravell

        #4
        Re: C# and VC6 Web Services File Upload

        If all else fails, try using a handler that uses your existing Files
        etc code via the context.Request ; I suspect that asmx isn't doing
        anything for you if you are using ASPUpload etc. It simply isn't the
        same type of service.

        Marc

        Comment

        • Marc Gravell

          #5
          Re: C# and VC6 Web Services File Upload

          My only thought is that there are better ways of copying streams -
          i.e. so that you don't need to read it all into memory first.
          An example is given here:



          Marc

          Comment

          • Marc Gravell

            #6
            Re: C# and VC6 Web Services File Upload

            (I should have said - the stress is on CopyStream - you can ignore the
            GZip stuff...)

            Comment

            Working...