We present the photometric calibration of the Supernova Legacy Survey (SNLS) fields. The SNLS aims at measuring the distances to SNe Ia at (0.3<z<1) using MegaCam, the 1 deg^2 imager on the Canada-France-Hawaii Telescope (CFHT). The uncertainty affecting the photometric calibration of the survey dominates the systematic uncertainty of the key measurement of the survey, namely the dark energy equation of state. The photometric calibration of the SNLS requires obtaining a uniform response across the imager, calibrating the science field stars in each survey band (SDSS-like ugriz bands) with respect to standards with known flux in the same bands, and binding the calibration to the UBVRI Landolt standards used to calibrate the nearby SNe from the literature necessary to produce cosmological constraints. The spatial non-uniformities of the imager photometric response are mapped using dithered observations of dense stellar fields. Photometric zero-points against Landolt standards are obtained. The linearity of the instrument is studied. We show that the imager filters and photometric response are not uniform and publish correction maps. We present models of the effective passbands of the instrument as a function of the position on the focal plane. We define a natural magnitude system for MegaCam. We show that the systematics affecting the magnitude-to-flux relations can be reduced if we use the spectrophotometric standard star BD +17 4708 instead of Vega as a fundamental flux standard. We publish ugriz catalogs of tertiary standards for all the SNLS fields.