asp.net 服务端调用客户端脚本; asp.net 服务端将文件传给客户端; reponse.ContentType的取值;用OutputStream.Write返回文件,效率是WriteFile的10倍;download link click和OutputStream的比较;
服务端
ScriptManager.RegisterStartupScript(this, this.GetType(), "JSFunction", "clickAWord();", true);
客户端:
<script type="text/jacascript"> function clickAWord() { alert("Hello World");
document.getElementById("tempA").click(); //触发A元素的click事件,跨浏览器 } </script>
二、asp.net 服务端将文件传给客户端
Response.Redirect(fileName); //来自codeproject, whole EnglishSearch
三、repsonse.ContentType的可能取值
<?xml version="1.0" encoding="utf-8" ?> <!-- Inernet Media Types http://en.wikipedia.org/wiki/Internet_media_type --> <mediaTypes> <mediaType> <contentType>application/vnd.ms-excel</contentType> <name>Microsoft Excel (tm)</name> <refUrl>http://www.iana.org/assignments/media-types/application/vnd.ms-excel</refUrl> <fileExtensions> <fileExtension>.xls</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>application/msword</contentType> <name>Microsoft Word</name> <refUrl>http://www.iana.org/assignments/media-types/application/msword</refUrl> <fileExtensions> <fileExtension>.doc</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>application/vnd.ms-powerpoint</contentType> <name>Microsoft Powerpoint (tm)</name> <refUrl>http://www.iana.org/assignments/media-types/application/vnd.ms-powerpoint</refUrl> <fileExtensions> <fileExtension>.ppt</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>application/pdf</contentType> <name>Portable Document Format</name> <refUrl>http://www.iana.org/assignments/media-types/application/vnd.ms-powerpoint</refUrl> <fileExtensions> <fileExtension>.pdf</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>image/jpeg</contentType> <name>JPEG JFIF image</name> <fileExtensions> <fileExtension>.jpg</fileExtension> <fileExtension>.jpeg</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>image/gif</contentType> <name>GIF image</name> <fileExtensions> <fileExtension>.gif</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>image/vnd.microsoft.icon</contentType> <name>ICO image</name> <refUrl>http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon</refUrl> <fileExtensions> <fileExtension>.ico</fileExtension> </fileExtensions> </mediaType> <mediaType> <contentType>application/zip</contentType> <name>ZIP file</name> <refUrl>http://www.iana.org/assignments/media-types/application/zip</refUrl> <fileExtensions> <fileExtension>.zip</fileExtension> </fileExtensions> </mediaType> </mediaTypes>
四、用OutputStream.Write返回文件,效率是WriteFile的10倍
txtReport:文件的路径
byte[] arByteFile=File.ReadAllBytes(txtReport); using (MemoryStream ms = new MemoryStream(arByteFile)) { long dataLengthToRead = ms.Length; int blockSize = dataLengthToRead >= 5000 ? 5000 : (int)dataLengthToRead; byte[] buffer = new byte[dataLengthToRead]; context.Response.Clear(); // Clear the content of the response context.Response.ClearContent(); context.Response.ClearHeaders(); // Buffer response so that page is sent // after processing is complete. context.Response.BufferOutput = true; // Add the file name and attachment, // which will force the open/cance/save dialog to show, to the header context.Response.AddHeader("Content-Disposition", "attachment; filename=导入报告.txt" ); // bypass the Open/Save/Cancel dialog //Response.AddHeader("Content-Disposition", "inline; filename=" + doc.FileName); // Add the file size into the response header context.Response.AddHeader("Content-Length", arByteFile.Length.ToString()); // Set the ContentType context.Response.ContentType = "application/octet-stream"; // Write the document into the response while (dataLengthToRead > 0 && context.Response.IsClientConnected) { Int32 lengthRead = ms.Read(buffer, 0, blockSize); context.Response.OutputStream.Write(buffer, 0, lengthRead); context.Response.Flush(); dataLengthToRead = dataLengthToRead - lengthRead; } context.Response.Flush(); context.Response.Close(); } context.Response.End();
//.net response header
五、download link click和OutputStream的比较;
方式1: 服务端Output.Stream.Write(byte[]) //以二进制流返回要下载的文件
方式2:服务端返回文件的uri,客户端ajax接收后, <a id="tempA">; tempA.href=url; temA.click() ;实现下载;
方式2的<a>叫download link。 href=*.xls,*.doc,*.zip 都会弹出下载对话框。
href=*.txt, 则会使当前页的内容变成txt的内容. 这就是downloadLink的局限性。