1

What I want to do:

Upload a file to autoCAD server and display a URN received from the server.

What I am currently doing:

Upload the file using <asp:FileUpload> control;

Get the uploaded file object from code behind using:HttpPostedFile myFile = FileUpload1.PostedFile;

The code behind will return a string called FileURN, it will be shown in a textarea using:

<textarea class="form-control" readonly="readonly" rows="5" id="txt_resUploadFile"><%= FileURN %></textarea>

The problem:

The html page gets refreshed every time the upload button is clicked. I understand that the full page gets refreshed when a trip to server is made. So I wanted to use AJAX for that. But soon I realize AJAX calls only Webmethod, which has to be static. Since HttpPostedFile myFile = FileUpload1.PostedFile; is no longer working in a static method, How can I get uploaded file stream?

Code Snippet:

html:

<div>
<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br/>
        <asp:Button class=" btn btn-primary col-md-2" ID="btnUpload" runat="server" Text="Upload file" onclick="btnUpload_Click" />
    </div>
</form>
<textarea class="form-control" readonly="readonly" rows="5" id="txt_resUploadFile"><%= FileURN %></textarea>

Codebehind:

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            HttpPostedFile myFile = FileUpload1.PostedFile;
            //send myFile to autoCAD server and get result from that server....
        }
    FileURN = "The Result";
Brian
  • 53
  • 1
  • 11
  • you can try using asp.net AJAX. have a look at this http://www.codingfusion.com/Post/AjaxFileUpload-example-to-upload-multiple-files-in and this http://www.codeproject.com/Articles/667406/File-Upload-Control-with-ASP-NET-and-AJAX – Sushil Sep 16 '15 at 17:00
  • Can you be more specific what are you trying to achieve?If you still use c# to get the stream you can follow this answer http://stackoverflow.com/questions/3068303/fileupload-to-filestream –  Sep 16 '15 at 17:03
  • Hi @Developer, thanks for the editing suggestion first. My ultimate goal is to to send a PUT RestRequest to autoCAD server, the requestBody Parameter carries the filestream data (in byte array) of the file I uploaded from `asp:FileUpload` control. The problem I have here is if I use C# onClick function, when the function is finished, a full page refresh will be called, which is something I don't want. – Brian Sep 16 '15 at 18:21
  • With c# onclick you cannot avoid post back.however can you use updatepanel or I think client side Ajax solution would fit nice for this case. –  Sep 16 '15 at 18:26
  • using(var client = new System.Net.WebClient()) { client.UploadData(address,"PUT",data); } –  Sep 16 '15 at 18:28

0 Answers0