0

I have written a fileupload control in asp.net mvc.But as of now the logic what i am doing is, i am downloading excel file in to server locally and then processing data and pushing in to oracle database.But some servers due to permission issues , i am not able to retrieve data from downloaded server or download in to server in either way. I would like to have a logic which will convert the uploading files in to a stream or what ever rather downloading in to server locally .I dont know whether i can implement HttpPostedFileBase.inputstream if so how can i implement it here?

VIEW

    @using (Html.BeginForm("Upload", "mnis", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @*<div>
        <label for="file">Filename:</label></div>
        <div class="uploadfirst">
        <input type="file" name="file" id="file" /></div>

        @Html.Label("lbl", "PSITE_SLNO");
        <input id="Text1"  name="txtsln" type="text" />
        <div class="upload">
        <input value="Submit"  type="submit" /></div>*@

        <table>
            <tr><td><label for="file">Filename:</label></td><td><input type="file" name="file" id="file" /></td></tr>
            <tr><td></td><td class="positions"><input value="Submit"    type="submit" /></td></tr>
        </table>

    }

CONTROLLER
 [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            try
            {

                if (file.ContentLength > 0)
                {

                    int slno = (int)(Session["slnum"]);
                    string st = file.FileName.ToString();
                    //st = Server.MapPath("~/")+st;
                    st = Path.Combine(Server.MapPath("~/"), st);
                    file.SaveAs(st);
                    //Start(st, slno);

                    Task.Factory.StartNew(() =>
                    {
                        Thread.CurrentThread.Name = "Maximo thread";

                        Start(st, slno);
                    });
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    RedirectToAction("Index");
                    //  file.SaveAs(path);
                }
            }
1
  • i posted answer try that one Commented Jun 13, 2015 at 16:40

1 Answer 1

1

try this:

You can Get Stream From InputStream of Fileuplpad Control then you can convert Stream to byte Array for saving into database

    Stream stream = file.PostedFile.InputStream;
    byte[] byteArray = ReadFully(stream);



 public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[input.Length];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.