访问Javascript变量

时间:2017-09-27 15:56:02

标签: javascript jquery ajax

我有这段代码:

$('button#monbtn').click(function() {
  var username = $('#username').val();
  var msg = $('#msg').val();

  var url = "{{ path('ajax_subcategories', {'username': username,'msg':msg})|escape('js') }}";
}

我从输入中获取用户名和消息,我想将它们添加到网址,但是我收到一个错误,说变量用户名不存在。

2 个答案:

答案 0 :(得分:1)

您没有编写用户名变量,而是将字username写入字符串。因此模板解析器(我猜)说它本身不知道这个var。

在JS中,你必须打印像这样的变种:

var url = "{{ path('ajax_subcategories', {'username': "+username+",'msg':"+msg+"})|escape('js') }}";

答案 1 :(得分:0)

您的变量不会被识别为字符串。将变量连接到最终的字符串url。

**--HttpClient (console Application)**  

                Dictionary<int, byte[]> fileparts = new Dictionary<int, byte[]>();
                int bufferSize = 1000 * 1024;

                byte[] buffer;
                string filePath = @"D:\Extra\Songs\test.mp3";
                using (FileStream fileData = File.OpenRead(filePath))
                {
                    int index = 0;
                    int i = 1;
                    while (fileData.Position < fileData.Length)
                    {

                        if (index + bufferSize > fileData.Length)
                        {
                            buffer = new byte[(int)fileData.Length - index];
                            fileData.Read(buffer, 0, ((int)fileData.Length - index));
                        }
                        else
                        {
                            buffer = new byte[bufferSize];
                            fileData.Read(buffer, 0, bufferSize);
                        }

                        fileparts.Add(i, buffer);
                        index = (int)fileData.Position;
                        i++;
                    }
                }
                
 while (fileparts.Count() != 0)
 {               
      var data = fileparts.First();          
  var fileContent = new ByteArrayContent(data);//byte content
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "test" };
                        fileContent.Headers.Add("id", id);
                        fileContent.Headers.Add("name", name);
                        fileContent.Headers.Add("length", length);
                        if (fileparts.Count() == 1)
                          fileContent.Headers.Add("IsCompleted", "true");
                        else
                          fileContent.Headers.Add("IsCompleted", "false");
                          
                    using (var content = new MultipartFormDataContent())
                    {
                       content.Add(fileContent);
                       // Make a call to Web API 
                        var result = client.PostAsync("/api/file", fileContent).Result;
                        if (result.StatusCode == System.Net.HttpStatusCode.OK)
                            fileparts.Remove(data.Key);
                    }

**--WebApi Core ApiController**

    public class FileController : Controller
    {
        [HttpPost]
        public async Task<IActionResult> Post()
        {
    /*i need to get the content here , for some reason existing .NET libraries does not work here
    like 
        MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
        FilePart part = null;
       
        await Request.Content.ReadAsMultipartAsync(provider); -- request.content not available 
        using (Stream fileStream = await provider.Contents[0].ReadAsStreamAsync())
                {
                    part = provider.Contents[0].Headers.GetData(); //public static FilePart GetData name,length,id 
                    part.Data = fileStream.ReadFully();
    */    
    }