asp.net mvc + JavaScript import file contents

Source: Internet
Author: User

。 What has been done recently is to refactor existing projects. The WEB from to MVC, in fact, is also a push.

There is an import function that uploads the file. The original approach was to have a hidden iframe, set up a form in this IFRAME's page form,form has a file upload control. And on the server side, after receiving the upload file, really save the file first, and then read it.

What a strange thing to do. Read the contents of the user's file, and then use it, do not have to really save the file first, because this is a temporary file, there is no value to save. Can be uploaded directly to the contents of the file read out, disposable.

Secondly, I understand why we used a hidden iframe. Because the upload file needs to be submitted, and the submission page will cause a refresh, in order to avoid the page content refresh, so got a hidden iframe, let it be responsible for submitting. Good is good, is to get more than one page.

In the new project, the front-end, the file is submitted by Ajax, server-side, receive files directly after processing, do not save, disposable:

Front:

<!--hidden upload control, onchange trigger-->
<input type= "file" id= "Fileimportdata" name= "Fileimportdata" onchange= " F_pointxy.importdata () "style=" Display:none/>  

<!--import button, click to trigger the upload control Click event, pop-up file selection box-->
<input Type= "button" value= "Import" class= "BTN Mini minilt" onclick= ' Javascript:document.getElementById "(" Fileimportdata "). Click (); '/>
<script type= "Text/javascript" >
    var f_pointxy = function () {

        function importdata () {
            $ ("#formImport "). Ajaxsubmit ({
                URL:" @Url. Staticfile ("~/common/yonghai/importdata/") "+ $ (" #txt_SMID "). Val (),
                type: ' Post ',
                data: {},
                success:function (data) {
                    if (data.substr (0, 2) = = "OK") {
                        alert ("Import succeeded");
                    } else { C10/>alert (data);
                    }
                ,
                error:function (e) {
                    alert (e);}
            });

        return {
            importdata:function () {
                importdata ();}}
        ;

    } ();
</script>

Server side:

 [HttpPost] public actionresult importdata (int id) {if (Reque St.
            Files.count = = 0 {return Content ("Failed to receive upload data", "text/html");

            String msg = "OK";
            HttpPostedFileBase fb = request.files[0]; string[] Acs_filepath = fb.
            Filename.split (' \ \ ');
            String acs_filename = Acs_filepath[acs_filepath.length-1]; String acs_filetype = Acs_filename.split ('. ') [1].
            ToLower (); String xytype = request["Xytype"]??

            ""; using (Stream fs = fb. InputStream) {int fileLen = fb.
                    ContentLength;
                    byte[] input = new Byte[filelen]; Fs.
                    Read (input, 0, fileLen);
                    String str = System.Text.Encoding.UTF8.GetString (input);
            msg + str;
        } End:return Content (msg, "text/html"); }

In this way, click the Import button, you can upload and import.

Monkey Thunder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.