Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Output a file like url via routing
Prepare a binary output action for file output
  1. [RoutePrefix("images")]
  2. public class ImageController : Controller
  3. {
  4.     // GET: ~/images/profile.png
  5.     // GET: ~/images/1873429732.jpg
  6.     [HttpGet]
  7.     [Route("{name}.{ext}")]
  8.     public ActionResult ReadImage(string name, string ext)
  9.     {
  10.         if (System.IO.File.Exists(Server.MapPath(string.Format("~/uploads/{0}.{1}", name, ext))))
  11.         {
  12.             byte[] data = System.IO.File.ReadAllBytes(Server.MapPath(string.Format("~/uploads/{0}.{1}", name, ext)));
  13.             string contentType = MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(string.Format("{0}.{1}", name, ext)));
  14.             return File(data, contentType);
  15.         }
  16.         else
  17.         {
  18.             return new HttpStatusCodeResult(404);
  19.         }
  20.     }
  21. }

Then try test it, but respone 404 not found.




Add runAllManagedModulesForAllRequests="true" on modules section.


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   ...
  4.   <system.webServer>
  5.     ...
  6.     <modules runAllManagedModulesForAllRequests="true">
  7.       <remove name="ApplicationInsightsWebTracking"/>
  8.       <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
  9.         preCondition="managedHandler"/>
  10.     </modules>
  11.   </system.webServer>
  12.   ...
  13. </configuration>

Then try it again, image rendered success.


Install subversion with apache server on linux (ubuntu)
# 更新 apt package
  1. sudo apt-get update
# 安裝 package
  1. sudo apt-get install subversion apache2 libapache2-svn apache2-utils
# 建立 svn 資料夾
  1. sudo mkdir -p /svn/repos/
# 建立 svn 使用者 (apache)
  1. sudo mkdir -p /svn/users/
# 建立使用者(第一次時有參數-c)
  1. sudo htpasswd -cm /svn/users/svnpasswd nanashi07
# 刪除使用者後再重建(重設密碼)
  1. #sudo htpasswd -D /svn/users/svnpasswd nanashi07
  2. #sudo htpasswd -m /svn/users/svnpasswd nanashi07
# 修改 apache 設定
  1. sudo vi /etc/apache2/mods-enabled/dav_svn.conf
  1. <Location /svn>
  2. DAV svn
  3. SVNParentPath /svn/repos/
  4. AuthType Basic
  5. AuthName "SVN Repository"
  6. AuthUserFile /svn/users/svnpasswd
  7. Require valid-user
  8. </Location>
# 修改 apache 監聽埠
  1. sudo vi /etc/apache2/ports.conf
# 重新啟動 apache
  1. sudo service apache2 restart
# 建立資源庫
  1. sudo svnadmin create /svn/repos/myproject
# 設定資源庫權限
  1. sudo chown -R www-data:www-data /svn/repos/myproject
# 設定資源庫權限
  1. sudo chmod -R g+rws /svn/repos/myproject
Add file to google drive
  1. using Google.Apis.Auth.OAuth2;
  2. using Google.Apis.Drive.v2;
  3. using Google.Apis.Drive.v2.Data;
  4. using Google.Apis.Services;
  5. using Google.Apis.Util.Store;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13.  
  14. namespace SyncDump
  15. {
  16.     class Program
  17.     {
  18.         /// <summary>
  19.         /// 存取權限
  20.         /// </summary>
  21.         static string[] Scopes = { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
  22.         static string ApplicationName = "Drive Uploader";
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             // 檢查輸入參數
  27.             if (args.Length < 1)
  28.             {
  29.                 Console.WriteLine("No target file argument!");
  30.                 return;
  31.             }
  32.  
  33.             // 要上傳的檔案
  34.             string dumpPath = args[0];
  35.             string dumpName = System.IO.Path.GetFileName(dumpPath);
  36.  
  37.             // 載入驗證資訊
  38.             GoogleCredential credential;
  39.             using (var stream = new System.IO.FileStream("client_secret.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
  40.             {
  41.                 credential = GoogleCredential.FromStream(stream);
  42.             }
  43.  
  44.             // 建立 service
  45.             DriveService service = new DriveService(new BaseClientService.Initializer()
  46.             {
  47.                 HttpClientInitializer = credential.CreateScoped(Scopes),
  48.                 ApplicationName = ApplicationName
  49.             });
  50.             
  51.             // 進行上傳
  52.             insertFile(
  53.                 service,
  54.                 dumpName,
  55.                 dumpName,
  56.                 System.Configuration.ConfigurationManager.AppSettings["drive.folder.id"],
  57.                 "application/oct-stream",
  58.                 dumpPath
  59.             );
  60.         }
  61.  
  62.         /// <summary>
  63.         /// 新增檔案
  64.         /// </summary>
  65.         /// <param name="service">Drive API service instance.</param>
  66.         /// <param name="title">Title of the file to insert, including the extension.</param>
  67.         /// <param name="description">Description of the file to insert.</param>
  68.         /// <param name="parentId">Parent folder's ID.</param>
  69.         /// <param name="mimeType">MIME type of the file to insert.</param>
  70.         /// <param name="filename">Filename of the file to insert.</param><br>  /// <returns>Inserted file metadata, null is returned if an API error occurred.</returns>
  71.         private static File insertFile(DriveService service, string title, string description, string parentId, string mimeType, string filename)
  72.         {
  73.             // 檔案資訊
  74.             File body = new File();
  75.             body.Title = title;
  76.             body.Description = description;
  77.             body.MimeType = mimeType;
  78.  
  79.             // 設定上傳位置
  80.             if (!string.IsNullOrEmpty(parentId))
  81.             {
  82.                 body.Parents = new List<ParentReference>() { new ParentReference() { Id = parentId } };
  83.             }
  84.  
  85.             // 檔案內容
  86.             byte[] byteArray = System.IO.File.ReadAllBytes(filename);
  87.             System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
  88.             try
  89.             {
  90.                 FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
  91.                 request.Upload();
  92.  
  93.                 File file = request.ResponseBody;
  94.  
  95.                 Console.WriteLine("{0:yyyy/MM/dd HH:mm:ss} File {1} is uploaded to drive", DateTime.Now, file.Title);
  96.  
  97.                 return file;
  98.             }
  99.             catch (Exception e)
  100.             {
  101.                 Console.WriteLine("An error occurred: " + e.Message);
  102.                 return null;
  103.             }
  104.         }
  105.     }
  106. }