jQuery File Upload以超链接形式返回文件路径

时间:2018-11-23 07:30:48

标签: jquery ajax webmethod

1。我正在尝试使用ajax和jquery插件上传文件,我想创建一个超链接,该链接允许用户打开上传的文件,我的服务器端代码在下面

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NEW : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string pathh = @"../../upload";
        var httpPostedFile = HttpContext.Current.Request.Files["Uploads"];
        if (httpPostedFile != null)
        {
            var filename = httpPostedFile.FileName;
            var fileSavePath = 
            Path.Combine(HttpContext.Current.Server.MapPath("~/upload"), 
            filename);
            var retpath = fileSavePath;

            httpPostedFile.SaveAs(fileSavePath);
            string newPath = Path.GetFullPath(pathh);
            newPath = Path.Combine(newPath, filename);
            Response.Clear();
            Response.Write(newPath);
            Response.End();

        }



    }
}

2。单击后调用的函数在下面,如果返回字符串并使其成为超链接,则该函数不显示任何内容。

        $(function () {
            $('#btnUpload').click(function () {

                var data = new FormData();
                var files = $("#fileupload").get(0).files;
                if (files.length > 0) {
                    data.append("Uploads", files[0]);
                }
                $.ajax({
                    url: "NEW.aspx",
                    type: "POST",
                    contentType:false,
                    processData: false,
                    data: data,
                    success: function (data) {
                        alert(data);
                        alert("uploaded");

                    },
                    error: err

                });

                function err(data) {
                        alert("error");
                    }
            });
        });

0 个答案:

没有答案