During the Dungan Revolt of 1862–1877, the Russian Empire occupied the Ili region in Xinjiang. After Qing imperial forces successfully crushed the Dungan Revolt, they demanded that the Russians withdraw from Ili, which led to the Ili Crisis.
While serving as the governor of Nanjing in 1894, Zhang invited a German training regiment of 12 officers and 24 warrant officers to train the local garrison into a modern military force. In 1896, acting under an imperial decree, Zhang moved to Wuchang to serve as the Viceroy of Huguang, an area comprising Hubei and Hunan provinces. Zhang drew on his experience in Nanjing to modernise the military forces under his command in Huguang.
Zhang held on a strong opinion on the issue of ceding Taiwan to the Japanese, per the 1895 Treaty of Shimonoseki that ended the First Sino-Japanese War. In late February 1895, he made his stance clear to the Qing government, and even offered ideas on how to prevent the loss of Taiwan. He suggested that they take huge loans from the British, who would in turn send their navy to defend Taiwan from the Japanese. In addition, he proposed giving mining rights to the British on Taiwan for about 10 to 20 years. In May 1895, the Qing government ordered all civil and military officials to evacuate Taiwan. Zhang also refused to provide aid to the remaining Qing forces in Taiwan, especially after the fall of Keelung and with Taipei as the sole remaining Qing stronghold in Taiwan. On 19 October 1895, the last of the Qing forces in Taiwan, led by Liu Yongfu, withdrew to Xiamen.
In 1898, Zhang published his work, Exhortation to Study (勸學篇). He insisted on a method of relatively conservative reform, summarized in his phrase "Chinese learning for fundamental principles and Western learning for practical application" (中學為體，西學為用). In 1900, he advocated the suppression of the Boxer Rebellion. When the Eight-Nation Alliance entered Beijing, Zhang, along with Li Hongzhang and others, participated in The Mutual Protection of Southeast China. He quelled local revolts and defeated the rebel army of Tang Caichang. He succeeded Liu Kunyi as Viceroy of Liangjiang in 1901, and moved to Nanjing, where he laid the foundations for the modern University of Nanjing. He was appointed the Minister of Military Affairs in 1906, and worked in Beijing for the central government.
Zhang's troops later became involved in politics. In 1911, the Wuchang garrison led the Wuchang Uprising, a coup against the local government that catalysed the nationwide Xinhai Revolution. The Xinhai Revolution led to the collapse of the Qing dynasty and its replacement by the Republic of China.